您的代码示例将不起作用,因为通用方法需要类型标识符,而不是Type类的实例。您必须使用反射来做到这一点:
public class Example { public void CallingTest() { MethodInfo method = typeof (Example).GetMethod("Test"); MethodInfo genericMethod = method.MakeGenericMethod(typeof (string)); genericMethod.Invoke(this, null); } public void Test<T>() { Console.WriteLine(typeof (T).Name); }}请记住,这非常脆弱,我宁愿建议您找到另一种模式来调用您的方法。
另一个骇人听闻的解决方案(也许有人可以使其变得更干净)将使用一些表达魔术:
public class Example { public void CallingTest() { MethodInfo method = GetMethod<Example>(x => x.Test<object>()); MethodInfo genericMethod = method.MakeGenericMethod(typeof (string)); genericMethod.Invoke(this, null); } public static MethodInfo GetMethod<T>(expression<Action<T>> expr) { return ((MethodCallexpression) expr.Body) .Method .GetGenericMethodDefinition(); } public void Test<T>() { Console.WriteLine(typeof (T).Name); }}请注意在lambda中将“对象”类型标识符作为通用类型参数传递。无法如此迅速地找到解决方法。无论哪种方式,我认为这都是编译时安全的。只是感觉不对:/


![如何使用给定的Type对象调用泛型方法?[重复] 如何使用给定的Type对象调用泛型方法?[重复]](http://www.mshxw.com/aiimages/31/568411.png)
