天天看點

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);      
StructureMap.dll 中的 GetInstance 重載 + 如何利用 反射動态建立泛型類

作者:

KMSFan

出處:http://www.cnblogs.com/kmsfan

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

歡迎大家加入KMSFan之家,以及通路我的優酷空間!