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?
A generic method may be overloaded like any other method. A class can provide two or more generic methods that specify the same method name but different method parameters. For example, generic method printArray of Fig.
Answer: Yes, we can overload a generic methods in C# as we overload a normal method in a class. Q- If we overload generic method in C# with specific data type which one would get called? Answer: Function with specific data type i.e int will be called.
Method overloading can be done in a web service with the following things: By changing the number of parameters used. By changing the order of parameters. By using different data types for the parameters.
Why don't you use expression trees? This makes it much easier:
public static MethodInfo GetMethod<T>(
Expression<Action<T>> methodSelector)
{
var body = (MethodCallExpression)methodSelector.Body;
return body.Method;
}
[TestMethod]
public void Test1()
{
var expectedMethod = typeof(Foo)
.GetMethod("Bar", new Type[] { typeof(Func<>) });
var actualMethod =
GetMethod<Foo>(foo => foo.Bar<object>((Func<object>)null)
.GetGenericMethodDefinition();
Assert.AreEqual(expectedMethod, actualMethod);
}
Surprisingly, it looks like you'll need to call GetMethods()
and loop over the methods until you find the one you want.
For example:
var yourMethod = typeof(Foo).GetMethods()
.First(m => m.Name == "Bar"
&& m.GetParameters().Length == 1
&& m.GetParameters()[0].ParameterType.ContainsGenericParameters
&& m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(Func<>));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With