日期:2014-05-17  浏览次数:20822 次

如何反射重载的泛型方法???

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));