如何获得一个泛型方法的MethodInfo的在一个非一般的.NET类型? [英] How to get MethodInfo of a generic method on a non generic .NET type?

查看:162
本文介绍了如何获得一个泛型方法的MethodInfo的在一个非一般的.NET类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的小问题,我想不出来传递给Type.GetMethod为了找回泛型方法的MethodInfo的在非泛型类型哪些参数。 具体而言,我有这种类型的定义:

 公共静态A级
{
  公共静态乙F< T>(布尔假)
  {
  }
  公共静态乙F< T>(IEnumerable的< T> ARG)
  {
  ...
  }
}
 

我尝试了好几种需要在Type.GetMethod,但没有将返回在F方法的MethodInfo。

我知道我可以调用Type.GetMethods甚至Type.FindMember,但我感兴趣的Type.GetMethod。

任何想法?

感谢。

修改

其实,我的code是一个有点复杂。通用方法是重载的,所以我不能用Type.GetMethod只用函数名。 我想这些变种:

 的typeof(A).GetMethod(F,BindingFlags.Static | BindingFlags.Public,空,新类型[] {typeof运算(IEnumerable的<>)},空)
typeof运算(A).GetMethod(F`1,BindingFlags.Static | BindingFlags.Public,空,新类型[] {typeof运算(IEnumerable的<>)},空)
typeof运算(A).GetMethod(F [T],BindingFlags.Static | BindingFlags.Public,空,新类型[] {typeof运算(IEnumerable的<>)},空)
typeof运算(A).GetMethod(F [T]],BindingFlags.Static | BindingFlags.Public,空,新类型[] {typeof运算(IEnumerable的<>)},空)
 

解决方案

的问题是,的IEnumerable<> 参数要传递到 GetMethod的不专业。这真的是一个的IEnumerable< T> ,其中 T 是您要检索方法指定。但是,我们不能 T 通过 MethodInfo.GetGenericArguments(),因为我们没有一个参考方法 - 我们仍在努力找回它

不幸的是,这是在反射API达不到。没有 Type.GetMethod()过载,使您可以重载的方法加以区分,其中一个是一个通用的方法。

所以,随着中说,你正在使用 Type.GetMethods卡()和过滤结果与您所选择的predicate。为了让你感兴趣的方法,你可以做到以下几点。

 无效GetMethod的()
{
    typeof运算(A).GetMethods()式(M =>
        m.IsGenericMethod&功放;&安培;
        m.GetParameters()[0] .ParameterType.GetGenericTypeDefinition()
            == typeof运算(IEnumerable的<>));
}
 

N.B。我还没有证实,该 GetGenericTypeDefinition()调用是必需的;你可以忽略它。这个想法是,您要变换类型 A< T> A<> ,但运行时可能已给你以这样的形式。

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)

解决方案

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.

这篇关于如何获得一个泛型方法的MethodInfo的在一个非一般的.NET类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆