Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-30
5
Medium Priority
?
324 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

721 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