If I want to call a generic method through reflection, I can easily use this technique, unless:
- The method can only be distinguished from another by its parameters.
- The method has a parameter that's type is one of the method's type parameters.
How do I specify a generic parameter in the Type[]
array when calling Type.GetMethod(string, Type[])
?
Example:
public class Example
{
//This is the one I want to call.
public void DoSomething<T>(T t) { ... }
public void DoSomething(Foo foo) { ... }
public void CallDoSomething(Type type, object value)
{
MethodInfo method = typeof(Example)
.GetMethod("DoSomething", new Type[] {/* what do i put here? */ });
MethodInfo generic = method.MakeGenericMethod(type);
generic.Invoke(this, value);
}