1、实例化类: 转自 https://www.coder.work/article/226551
public class MyGenericClass<T>
string typeName = "System.String";
Type typeArgument = Type.GetType(typeName);
Type genericClass = typeof(Generic<>);
// MakeGenericType is badly named
Type constructedClass = genericClass.MakeGenericType(typeArgument);
object created = Activator.CreateInstance(constructedClass);
2、运行方法: 转自 https://blog.csdn.net/guce2885/article/details/108197120
public class YamlConverter
{
public object Deserializer<T>(string input)
{
object result = null;
// DO something
return result;
}
}
// 外部函数调用此方法
public static object Create(string id,string content)
{
Type type = TestMap.Map[id];
YamlConverter yaml = new YamlConverter();
MethodInfo mi = yaml.GetType().GetMethod("Deserializer").MakeGenericMethod(new Type[] { type });
object[] l_args = new object[1] { content };
object testContent = mi.Invoke(yaml, l_args);
return testContent;
}