Retrieving the MethodInfo of of the correct overload of a generic method
Posted
by
Anne
on Stack Overflow
See other posts from Stack Overflow
or by Anne
Published on 2010-12-14T19:34:18Z
Indexed on
2011/01/06
19:54 UTC
Read the original article
Hit count: 201
I have this type that contains two overloads of a generic method. I like to retrieve one of the overloads (with the Func<T>
parameter) using reflection. The problem however is that I can't find the correct parameter type to supply the Type.GetMethod(string, Type[])
method with.
Here is my class definition:
public class Foo
{
public void Bar<T>(Func<T> f) { }
public void Bar<T>(Action<T> a) { }
}
And this is what I've come up with, unfortunately without succes:
[TestMethod]
public void Test1()
{
Type parameterType = typeof(Func<>);
var method = typeof(Foo).GetMethod("Bar", new Type[] { parameterType });
Assert.IsNotNull(method); // Fails
}
How can I get the MethodInfo
of a generic method of which I know the parameters?
© Stack Overflow or respective owner