程序集加载与反射笔记

            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);
            }
posted @ 2013-08-22 10:00  中本傻  阅读(279)  评论(0编辑  收藏  举报