Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

770 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