您的位置:首页 > 编程语言 > C#

深入理解 c# 第三章 通过反射来调用和获取泛型方法

2018-04-07 23:43 736 查看
class GenericMethodReflection
{
public static void PrintTypeParameter<T>()
{
Console.WriteLine (typeof(T));
}

static void Main()
{
Type type = typeof(GenericMethodReflection);
MethodInfo definition = type.GetMethod("PrintTypeParameter");
MethodInfo constructed;
constructed = definition.MakeGenericMethod(typeof(string));
constructed.Invoke(null, null);
}
}


MakeGenericMethod返回一个已构造的泛型方法
在Type中,没有任何方法允许指定类型参数的数量

获取已构造方法后,可以调用,两个参数都是null ,要调用的是静态方法,它不获取任何"普通"的参数。

输出
System.String
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#