StructureMap.dll 中的 GetInstance 重载 + 如何利用 反射动态创建泛型类

        public static T GetInstance<T>(ExplicitArguments args);
//
// Summary:
// Creates a new instance of the requested type T using the supplied Instance.
// Mostly used internally
//
// Parameters:
// instance:
public static T GetInstance<T>(Instance instance);
//
// Summary:
// Creates or finds the default instance of the pluginType
//
// Parameters:
// pluginType:
public static object GetInstance(Type pluginType);
//
// Summary:
// Creates a new instance of the requested type using the supplied Instance.
// Mostly used internally
//
// Parameters:
// targetType:
//
// instance:
public static object GetInstance(Type targetType, Instance instance);

用 反射动态 创建泛型类的方法 :

var d1 = Type.GetType("GenericTest.TaskA`1"); // GenericTest was my namespace, add yours
Type[] typeArgs = { typeof(Item) };
var makeme = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(makeme);
上一篇:C#创建泛型类T的实例的三种方法


下一篇:Take into Action!