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
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