首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-02-24 
如何反射重载的泛型方法???public static class TestClass{public static void TestMethodT(){}public s

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


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

热点排行