Generics and Reflection


B

brian

I have an application that dynamically invokes methods based on
information in an XML file. In order to do this, I first have to make
sure that a method with matching parameter types exists.

All of the usable methods have generic parameter types, but most of the
methods are not generic themselves, i.e.

public object MyMethod(Parameter<int> id, Parameter<string> name)

When the XML file is read, the data types are determined, and a Type
array of constructed generic types is formed. Just as example here this
code would basically do the same thing:

Type[] typeArray = new Type[] { typeof(Parameter<int>),
typeof(Parameter<string>) };

Then, I can successfully locate the method using

this.GetType().GetMethod(methodName, typeArray);

The problem I run across is when I create a generic method that has
generic parameter types of the same type as the method, such as:

public object MyGenericMethod<T>(Parameter<T> myParam)

Using the same GetMethod call as the one above, with the same Type
array, doesn't find any matching methods. So my question is, what
arguments would GetMethod() need in order to correctly locate this
method? Is there even a way to search for a particular form of a
generic method using GetMethod()?

Thanks in advance for any insight!


-Brian C. Stanford
 
Ad

Advertisements

D

Dave Sexton

Hi Brian,

Check out the following reference.

Type.IsGenericType property on MSDN:
http://msdn2.microsoft.com/en-us/library/system.type.isgenerictype(VS.80).aspx

I believe GetMethod doesn't find any methods that have a signature with the
specified generic, parameter Type because you're not dealing with an open
constructed type in the method signature, but instead a closed constructed
type. The closed constructed Type parameter cannot be obtained without first
obtaining a reference to the method because Type, "T" cannot be constructed
outside of the method. Type, "T" is simply a generic parameter and so the
actual method argument Type itself is not accessible through the Type.GetType
method.

The closest I believe that you'll get to resolving the appropriate generic
parameter Type required by the GetMethod method is the Type of the generic
type definition, but that's not enough.

However, I think that your goal can actually be accomplished, but not without
great difficulty; probably including multiple iterative loops that are needed
to resolve the generic method type arguments to those of the parameters
supplied in XML. If you don't need such "dynamic" requirements then you might
be able to find a simpler way, but if your XML may contain a varying number of
parameters, generic parameter arguments and generic method arguments then I
imagine that a good solution will be somewhat complex.

--
Dave Sexton

I have an application that dynamically invokes methods based on
information in an XML file. In order to do this, I first have to make
sure that a method with matching parameter types exists.

All of the usable methods have generic parameter types, but most of the
methods are not generic themselves, i.e.

public object MyMethod(Parameter<int> id, Parameter<string> name)

When the XML file is read, the data types are determined, and a Type
array of constructed generic types is formed. Just as example here this
code would basically do the same thing:

Type[] typeArray = new Type[] { typeof(Parameter<int>),
typeof(Parameter<string>) };

Then, I can successfully locate the method using

this.GetType().GetMethod(methodName, typeArray);

The problem I run across is when I create a generic method that has
generic parameter types of the same type as the method, such as:

public object MyGenericMethod<T>(Parameter<T> myParam)

Using the same GetMethod call as the one above, with the same Type
array, doesn't find any matching methods. So my question is, what
arguments would GetMethod() need in order to correctly locate this
method? Is there even a way to search for a particular form of a
generic method using GetMethod()?

Thanks in advance for any insight!


-Brian C. Stanford
 
Ad

Advertisements

B

brian

Yeah I've already written a fairly complex block of code to figure out
when the generic method's type and a generic parameter's type are
supposed to be the same, and I have a good idea where to go from here,
but I was hoping someone knew of a more straightforward approach before
I continued writing it.

Thanks for the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top