I was wondering if someone might be able to demonstrate how to use Type's GetMethod() method to retrieve a MethodInfo object for the following signature:
Class.StaticMethod(T arg1, IInterface1 arg2, IEnumerable arg3)
Thanks,
Xam
解决方案MethodInfo methodInfo = typeof(Class)
.GetMethods(
BindingFlags.Public | BindingFlags.Static
)
.Where(m => m.Name == "StaticMethod")
.Where(m => m.IsGenericMethod)
.Where(m => m.GetGenericArguments().Length == 1)
.Where(m => m.GetParameters().Length == 3)
.Where(m =>
m.GetParameters()[0].ParameterType ==
m.GetGenericArguments()[0] &&
m.GetParameters()[1].ParameterType ==
typeof(IInterface1) &&
m.GetParameters()[2].ParameterType ==
typeof(IEnumerable)
)
.Single();
Note that you must then follow this with
methodInfo = methodInfo.MakeGenericMethod(new Type[] { typeof(ConcreteType) });
to close the type where ConcreteType is the type you want for the type parameter T.
I'm assuming:
class Class {
public static void StaticMethod(
T arg1,
IInterface1 arg2,
IEnumerable arg3
) { }
}