我有这个小问题,我无法弄清楚要传递给 Type.GetMethod 的参数,以便在非泛型类型上取回泛型方法的 MethodInfo。具体来说,我有这个类型定义:
public static class A
{
public static B F<T>(bool dummy)
{
}
public static B F<T>(IEnumerable<T> arg)
{
...
}
}
我在 Type.GetMethod 上尝试了几次,但没有一个会返回 F 方法的 MethodInfo。
我知道我可以调用 Type.GetMethods 甚至 Type.FindMember,但我对 Type.GetMethod 很感兴趣。
有任何想法吗?
谢谢。
编辑
实际上,我的代码有点复杂。泛型方法被重载,所以我不能只使用函数名来使用 Type.GetMethod。我尝试了这些变体:
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)