I have this little problem, that I cannot figure out which arguments to pass to Type.GetMethod in order to get back the MethodInfo of a generic method on a non generic type. Specifically, I have this type definition:
public static class A
{
public static B F<T>(bool dummy)
{
}
public static B F<T>(IEnumerable<T> arg)
{
...
}
}
I have tried several takes at Type.GetMethod, but none would return the MethodInfo of the F method.
I am aware that I can invoke Type.GetMethods or even Type.FindMember, but I am interested in Type.GetMethod.
Any ideas?
Thanks.
EDIT
Actually, my code is a bit more complex. The generic method is overloaded, so I cannot use the Type.GetMethod with just the function name. I tried these variants:
typeof(A).GetMethod("F", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F`1", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F[T]", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
typeof(A).GetMethod("F[[T]]", BindingFlags.Static | BindingFlags.Public, null, new Type[]{ typeof(IEnumerable<>) }, null)
Yes, you can define a generic method in a non-generic class in Java.
Yes, There are two level where you can apply generic type . You can apply generic type on Method level as well as Class level (both are optional). As above example you applied generic type at method level so, you must apply generic on method return type and method name as well. You need to change a bit of code.
MethodInfo method = typeof(Foo). GetMethod("MyGenericMethod"); method = method. MakeGenericMethod(t); method. Invoke(this, new object[0]);
Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.
The problem is that the IEnumerable<>
parameter you are passing to GetMethod
is not specialized. It really is an IEnumerable<T>
, where T
is specified by the method you are trying to retrieve. But, we can't get T
via MethodInfo.GetGenericArguments()
since we don't have a reference to the method -- we are still trying to retrieve it.
Unfortunately, this is where the reflection API falls short. There is no Type.GetMethod()
overload that allows you to distinguish between overloaded methods, where one is a generic method.
So with that said, you are stuck using Type.GetMethods()
and filtering the results with a predicate of your choice. To get the method you are interested in, you can do the following.
void getMethod()
{
typeof(A).GetMethods().Where(m =>
m.IsGenericMethod &&
m.GetParameters()[0].ParameterType.GetGenericTypeDefinition()
== typeof(IEnumerable<>));
}
N.B. I haven't verified that the GetGenericTypeDefinition()
call is required; you may be able to omit it. The idea is that you are transforming a type A<T>
into A<>
, but the runtime may already give it to you in that form.
(Updated in response to question clarifications):
There is no way to get a handle to the method using GetMethod
(eg. one line), because the generic data to define the method isn't available until we have the method to inspect.
MethodInfo[] methods = typeof(A).GetMethods(BindingFlags.Static | BindingFlags.Public);
MethodInfo genericMethod = methods.Where(m=>m.IsGenericMethod).First(m=>m.ContainsGenericParameters);
genericMethod = genericMethod.GetGenericMethodDefinition();
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