当前位置: 代码迷 >> C# >> 怎么反射重载的泛型方法?
  详细解决方案

怎么反射重载的泛型方法?

热度:6801   发布时间:2013-02-25 00:00:00.0
如何反射重载的泛型方法???

public static class TestClass
{
  public static void TestMethod<T>(){}
  public static void TestMethod<T>(string name){}

  public static void TestMethod<T, S>(){}
  public static void TestMethod<T, S>(string name){}
}


如果只是反射参数不同的重载方法是没什么问题,求高手解答如何反射这种泛型不同的方法?

------解决方案--------------------------------------------------------
找到所有同名的方法,然后MethodInfo中有个IsGenericMethod属性判断是否泛型方法
------解决方案--------------------------------------------------------

            ParameterInfo[] types;
            MethodInfo method = typeof(LoginForm).GetMethods(BindingFlags.Static 
------解决方案--------------------------------------------------------
 BindingFlags.Public)
                .First(t => t.Name == "TestMethod" 
                        && t.IsGenericMethod
                        && t.GetGenericArguments().Length == 2
                        && (types = t.GetParameters()).Length == 1 && types[0].ParameterType == typeof(string));
  相关解决方案