Solved

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

Posted on 2007-11-30
5
316 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

679 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