Solved

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

Posted on 2007-11-30
5
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

628 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