今天一个地方要用到泛型方法,但是泛型里的类型只有runtime才知道,找了很久,发现可以利用反射去动态调用。 要用到泛型方法的类: public class DynamicVariables { public static void DynamicAddVariable<T>(ModelItem modelItem ,string name,T defaultValue) { ModelProperty modelProperty = modelItem.Properties["Variables"]; if (modelProperty != null) { Variable<T> var = new Variable<T>(name,defaultValue); modelProperty.Collection.Add(var); } } } 动态调用的方法: public static void InvokeStaticGenericMethod(object[] args,Type classType,string methodName,Type genericType) { try { MethodInfo mi = classType.GetMethod(methodName); MethodInfo miConstructed = mi.MakeGenericMethod(genericType); miConstructed.Invoke(null, args); } catch { throw; } }