Solved

How do I clear a generic collection using MethodInfo.Invoke?

Posted on 2007-11-30
5
305 Views
Last Modified: 2013-11-26
I have a base class and a number of child classes that derive from this class that contain collections which need to be cleared when I call my base class' dispose method.  I am attempting to implement a method that will dynamically search my custom classes for fields that are collections, and then call the "Clear"method on these collections to reclaim the resources, before it is garbage collected.  I a doing this so I dont'have to write the same function 20 times for each of my child classes.

I am able to reflect to the method I need, however when i attempt to invoke the method I get a target exception error and don't understand why.  My code is below:

Public Sub ClearCollections()

        Dim xField As FieldInfo, xfields As FieldInfo()

        Dim xType As Type

        Dim xMethod As MethodInfo

        Dim xType2 As Type

        Dim args() As Object
 

        xType = MyClass.GetType
 

        'First get the fields

        xfields = xType.GetFields
 

        For Each xField In xfields
 

            If xField.FieldType.FullName.ToUpper.Contains("COLLECTION") Then
 

                xType2 = xField.FieldType
 

                'Clear the collection

                xMethod = xType2.GetMethod("Clear")

                args = xMethod.GetParameters

                xMethod.Invoke(xField, Nothing)
 

            End If
 

        Next

    End Sub

Open in new window

0
Comment
Question by:csjordan
  • 3
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20384571
could you post the error message?
I forgot my crystal ball at home ;)
0
 

Author Comment

by:csjordan
ID: 20385413
Sure! Error message is "Object does not match target type."

Not sure if it matters but the collection is a system.collections.generic.dictionary.

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 20385680
how is defined MyClass?
is it a class or an object. If it is a class, you need an object to invoke, if it is an object, it should be something like:

          If xField.FieldType.FullName.ToUpper.Contains("COLLECTION") Then
                Dim o as Object
                o = xField.GetValue(MyClass)  ' here is the instance of the field
                ' get the method based on instance type
                xMethod = o.GetType().GetMethod("Clear")
                xMethod.Invoke(o, Nothing)
 
0
 

Author Comment

by:csjordan
ID: 20385951
It is a class... so are you saying I need to create an instance of the class?  How would I do this dynamically?
0
 

Author Comment

by:csjordan
ID: 20385972
That code snip you sent did the trick.

Thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now