Invoke an overloaded generic method with parameters using reflection
I have been working on an ASP.NET web forms application recently where I was using Unity as my inversion of control (IoC) container and I needed a way to build up my dependencies automatically. To do this I had to Invoke a generic method using reflection passing both the type arguments and parameters to the method. I won't focus on how I achieved automatic build up of my dependencies within my web application, I will only detail a component that helped me along the way.
public static class TypeExtensions
{
///
/// Invoke a generic method using reflection
///
/// Type containing the method for invocation
/// Method to invoke (case sensitive)
/// Generic type arguments
/// Method parameters
/// Object returned from method invocation call
public static object InvokeGenericMethod(
this Type type,
string methodName,
Type[] typeArguments,
object[] parameters)
{
// Since this method is for generic types
if(typeArguments == null || typeArguments.Length == 0)
throw new ArgumentException("Type arguments must be supplied", "typeArguments");
MethodInfo[] resolveMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
// Ensure that the method we invoke matches the
// name, type arguments count and number of parameters
var methodsMatchBasicCheck = resolveMethods
.Where(
mi => mi.Name == methodName &&
mi.GetGenericArguments().Count() == typeArguments.Length &&
(parameters != null)
? mi.GetParameters().Count() == parameters.Length
: mi.GetParameters().Count() == 1 /* At least 1 parameter (Extension instance) */)
.ToArray();
// Take first match.
// NOTE: Another filter is necessary to make a more precise
// selection when base on the type arguments and parameters
// there are multiple possible methods available.
MethodInfo resolveMethod = methodsMatchBasicCheck.FirstOrDefault();
if (resolveMethod == null)
return null;
// Invoke method
MethodInfo genericResolveMethod = resolveMethod.MakeGenericMethod(typeArguments);
return genericResolveMethod.Invoke(null, parameters);
}
}
I created an extension method for the Type class which takes the following parameters:
Now one of the most critical parts to any article is demonstrating how to use the code provided. Always important but quite often forgotten. The following example is contrived but demonstrates the usage.
Debug.WriteLine(
typeof (StringExtensions).InvokeGenericMethod
(
"Sum",
new[] {typeof (int), typeof (double)},
new object[] {"Sum: ", 5, 5.5D}
)
);
public static class StringExtensions
{
public static string Sum<T1, T2>(this String s, T1 val1, T2 val2)
where T1 : IConvertible
where T2 : IConvertible
{
return String.Format("{0}{1}", s,
(val1.ToInt32(CultureInfo.InvariantCulture.NumberFormat) +
val2.ToInt32(CultureInfo.InvariantCulture.NumberFormat)));
}
}
The StringExtensions class which really wanted to be a MathExtensions (the Math class is static so not an option) class contains the generic extension method with 3 parameters if you include the object of type being extended (this String s), two generic arguments and returns a string. With one simple call the method is invoked and the result of 11 is written to the debug window.
I hope you enjoyed the article and don't forget to leave feedback.
I really liked your article.Really looking forward to read more. Really Great.
This is the precise blog for anybody who wants to search out out about this topic. You notice a lot its virtually hard to argue with you (not that I truly would want…HaHa). You positively put a brand new spin on a topic thats been written about for years. Great stuff, simply nice!
If any one wishes to be a successful blogger, afterward he/she must look at this piece of writing, as it consists of al} techniques related to that.
The determination of type difference is not based on the generic type parameter, but on the type argument substituted for the type parameter when a constructed type is created.
After research a couple of of the weblog posts on your web site now, and I truly like your means of blogging. I bookmarked it to my bookmark web site list and shall be checking back soon. Pls take a look at my site as nicely and let me know what you think.