程序集加載與反射筆記

            Assembly.Load    //加載程序集,會從GAC、應用程序基目錄、私有路徑子目錄查找
            Assembly.LoadFrom    //從路徑加載,首先會獲得程序集,而後內部調用Assembly.Load
            Assembly.LoadFile    //可屢次加載一個程序集到AppDomain中,此時CLR不會自動解析任何依賴性問題
            Assembly.ReflectionOnlyLoadFrom        //若是隻想分析程序集的元數據,而不執行任何代碼,那麼這個方法再適合不過 了

            /*獲取程序集中定義的類型*/
            assembly.GetExportedTypes        //獲取有所公開導出的類型(程序集外部可見的類型,便是public類型)
                                            //返回值是System.Type數組
            Type.GetType        //接受一個String參數,首先會檢查調用程序集,看它是否認義了指定名稱的類型。若是找到就去MSCorLib.dll查找
            Type.ReflectionOnlyGetType //(僅反射)/type.GetNestedType/type.GetNestedTypes
            type.IsClass/IsPublic/IsAssignableFrom(...)

            /*構造類型的實例*/
            Activator.CreateInstance
            Activator.CreateInstanceFrom    //經過字符串來指定類型及程序集,會調用Assembly.LoadFrom加載程序集
                                            //返回值是ObjectHandle對象引用,須要調用ObjectHandle的Unwrap方法進行具體化
            AppDomain.CreateInstance/CreateInstanceAndUnwrap    //和Activator類方法相似,帶Unwrap後綴方法幫咱們簡化操做
            AppDomain.CreateInstanceFrom/CreateInstanceFromAndUnwrap
            type.InvokeMember    //該方法查找與傳遞的實參相匹配的一個構造器,並構造類型
            System.Relection.ConstructorInfo實例方法Invoke
           var type = Type.GetType("ConsoleApplication1.ReflectTest");

            /*①建立實例*/
            var instance = (ReflectTest)null;
            instance = Activator.CreateInstance(type) as ReflectTest;
            instance = Activator.CreateInstanceFrom("ConsoleApplication1.exe", "ConsoleApplication1.ReflectTest").Unwrap() as ReflectTest;
            instance = AppDomain.CurrentDomain.CreateInstanceAndUnwrap("ConsoleApplication1", "ConsoleApplication1.ReflectTest") as ReflectTest;
            instance = type.InvokeMember(null, BindingFlags.CreateInstance, null, null, null) as ReflectTest;
            instance = type.GetConstructor(Type.EmptyTypes).Invoke(null) as ReflectTest;

            /*②調用成員*/
            var result = (object)null;
            //直接訪問
            instance.Print("Hello World");
            //Type實例的InvokeMember方法【調用方法】
            type.InvokeMember("Print", BindingFlags.InvokeMethod, Type.DefaultBinder, instance, new object[] { "Hello World"});
            或者
            type.GetMethod("Print").Invoke(instance, new object[] { "Hello World2" });
            //Type實例的GetMember方法【調用屬性】
            result = type.GetProperty("SomeMsg").GetValue(instance);
            或者
            result = type.GetProperty("SomeMsg").GetGetMethod().Invoke(instance, null);
            或者
            result = (type.GetMember("SomeMsg", MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance)[0] as PropertyInfo).GetValue(instance);
            Console.WriteLine(result);
            //Type實例的GetField方法【調用私有字段】
            result = type.GetField("someMsg", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance);
            Console.WriteLine(result);

            /*反射獲取Attribute值*/
            var prop = type.GetProperty("SomeMsg");
            var descType =  typeof(DescriptionAttribute);
            if (Attribute.IsDefined(prop, descType))
            {
                var attr = Attribute.GetCustomAttribute(prop, descType) as DescriptionAttribute;
                Console.WriteLine(attr.Description);
            }
相關文章
相關標籤/搜索