Invoking Generic Methods with Generic Arguments

S

Suds

Hi,
I'm having an issue with invoking a Generic method that takes Generic Arguments.

My method signature is
public void GenericMethodWithGenericArguments<E, V>(List<E> theFirstList, List<V> theSecondList);

I pass the name of the method, the arguments for the "GenericMethodWithGenericArguments" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class.

My process of invocation is as follows

public static void InvokeGenericMethod(object theInstance, string theMethodToInvoke, object[] theMethodArguments, Type[] theArgumentTypes,
Type[] theArgumentParameterTypes)
{

MethodInfo testMethodToInvoke = null;
MethodInfo[] instanceMethods = null;

// Get all the instance methods
instanceMethods = theInstance.GetType().GetMethods();

// Go through each method in the instance
foreach (MethodInfo method in instanceMethods)
{

// Search for the method to inovke.
if (theMethodToInvoke.Equals(method.Name))
{
testMethodToInvoke = method.MakeGenericMethod(theArgumentTypes);
if (method.ContainsGenericParameters)
{
Type[] genericParameters = new Type[theArgumentTypes.Length];
for (int i = 0; i < genericParameters.Length; ++i)
{
if (theMethodArguments.GetType().IsGenericType)
{
Type genericDefinition = theArgumentTypes.GetGenericTypeDefinition();
genericParameters = genericDefinition.MakeGenericType(theArgumentParameterTypes);

}
else
{
genericParameters = theMethodArguments.GetType();
}
}
testMethodToInvoke = method.MakeGenericMethod(genericParameters);
}

}
}

try
{
// Call method under test
testMethodToInvoke.Invoke(theInstance, theMethodArguments);
}
catch (Exception e)
{
}

}

My method class is InvokeGenericMethod(instance, "GenericMethodWithGenericArguments", new object[] { list1, list2 }, new Type[] {typeof(List<int>), typeof(List<double>)}, new Type[]{typeof(int), typeof(double)});

The exception I get says {"Object of type 'System.Collections.Generic.List`1[System.Int32]' cannot be converted to type 'System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.Int32]]'."}.

The question I have is with the two lines below:

Type genericDefinition = theArgumentTypes.GetGenericTypeDefinition();
genericParameters = genericDefinition.MakeGenericType(theArgumentParameterTypes);
Why does my argument expected wrap the List into another List of int's instead of a List of int's? I'm not sure how to figure this out with the reading material I have. I went through the MSDN documentation and could not figure out why my arguments are failing.
Any help to fix this, or pointers to any documentation for this issue will be much appreciated.
Thanks
Suds
 
P

Peter Duniho

Hi,
I'm having an issue with invoking a Generic method that takes Generic
Arguments.

My method signature is
public void GenericMethodWithGenericArguments<E, V>(List<E>
theFirstList, List<V> theSecondList);

I'm having trouble parsing your code. You formatted it, which makes it
hard to read, and there seems to be a bunch of stuff in there not related
to your actual question, which makes it hard to focus on what actually is
important.

However, that said...just looking at your usage and your question, I am
guessing that the fundamental issue is a mistake in your generic method
declaration. That is, you have specified that the parameters will be
List<E> and List<V> types, and then the E and V types you pass in are
themselves lists. This results in the parameters being lists of lists. I
think what you actually want is this:

public void GenericMethodWithGenericArguments<E, V>(E, V) where E :
List<Object>, V : List<Object>;

Or something like that (generics are still new to me, and I'm not certain
about the exact syntax for the contraints). The idea being that you
constrain the parameters to types that are themselves generic lists
containing Object instances.

Pete
 

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