操作系统:Win7
编译器:VS2010
.net版本:.net4.0
namespace ReflectDemo { public class GetAssembly { public void MethodGetAllAssembly() { //获取当前应用程序域中的Assembly Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); Console.WriteLine(assemblies.ToString()); } public void MethodGetCurrentObjAssembly() { //获取当前对象所在的Assembly Assembly assembly = this.GetType().Assembly; Console.WriteLine(assembly.ToString()); } public void MethodGetFromBin() { //获取bin目录下的指定Assembly Assembly assembly = Assembly.LoadFrom("ReflectDemo.exe"); Console.WriteLine(assembly.ToString()); } } }
namespace ReflectDemo { public class GetType { public void MethodGetByClassName() { //通过类名获得Type Type type = typeof(Bird); Console.WriteLine(type.ToString()); } public void MethodGetByObjName() { //通过对象名获得Type Bird bird = new Bird(); Type type = bird.GetType(); Console.WriteLine(type.ToString()); } public void MethodGetByFullName() { //通过 命名空间.类名获取 Assembly assembly = this.GetType().Assembly; Type type = assembly.GetType("ReflectDemo.Bird"); } public void MethodGetAll() { //获取Assembly中所有的类型 Assembly assembly = this.GetType().Assembly; Type[] types = assembly.GetTypes(); } public void MethodGetAllPublic() { //获取Assembly中定义的所有public类 Assembly assembly = this.GetType().Assembly; Type[] types = assembly.GetExportedTypes(); } } }
namespace ReflectDemo { public class GetFieldInfo { public void MethodGetPublicField() { Bird bird = new Bird() { _id = "1" }; Type type = bird.GetType(); FieldInfo idInfo = type.GetField("_id"); string id = idInfo.GetValue(bird).ToString(); Console.WriteLine(id); idInfo.SetValue(bird, "2"); string newId = bird._id; Console.WriteLine(newId); } } }
namespace ReflectDemo { public class GetPropertyInfo { public void MethodGetAllPublic() { Bird bird = new Bird() { Name = "小黄" }; Type type = bird.GetType(); PropertyInfo nameInfo = type.GetProperty("Name"); string name = nameInfo.GetValue(bird, null).ToString(); Console.WriteLine(name); nameInfo.SetValue(bird, "小黄黄", null); Console.WriteLine(bird.Name); } } }
namespace ReflectDemo { public class GetMethodInfo { public void MethodGetWithNoParas() { Bird bird = new Bird(); Type type = bird.GetType(); MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { }); eatMethodInfo.Invoke(bird, null); } public void MethodWithParas() { Bird bird = new Bird(); Type type = bird.GetType(); MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { typeof(string) }); eatMethodInfo.Invoke(bird, new object[] { "小黑" }); } } }
namespace ReflectDemo { public class GetConstructorInfo { public void MethodGetActivator() { Type type = typeof(Bird); Bird bird = Activator.CreateInstance(type, new object[] { "小白", 3 }) as Bird; bird.BirdIntroducion(); } public void MethodGetConstructor() { Type type = typeof(Bird); ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(string), typeof(int) }); Bird bird = ctor.Invoke(new object[] { "小黑", 5 }) as Bird; bird.BirdIntroducion(); } } }
namespace ReflectDemo { class Program { static void Main(string[] args) { Console.WriteLine("---获取Assembly---"); GetAssembly getAssembly = new GetAssembly(); getAssembly.MethodGetAllAssembly(); getAssembly.MethodGetCurrentObjAssembly(); getAssembly.MethodGetFromBin(); Console.WriteLine("\n---获取Type对象---"); GetType getType = new GetType(); getType.MethodGetByClassName(); getType.MethodGetByObjName(); getType.MethodGetByFullName(); getType.MethodGetAll(); getType.MethodGetAllPublic(); Console.WriteLine("\n---获取字段信息---"); GetFieldInfo getFieldInfo = new GetFieldInfo(); getFieldInfo.MethodGetPublicField(); Console.WriteLine("\n---获取属性信息---"); GetPropertyInfo getPropertyInfo = new GetPropertyInfo(); getPropertyInfo.MethodGetAllPublic(); Console.WriteLine("\n---获取方法信息---"); GetMethodInfo getMethodInfo = new GetMethodInfo(); getMethodInfo.MethodGetWithNoParas(); getMethodInfo.MethodWithParas(); Console.WriteLine("\n---获取构造函数信息---"); GetConstructorInfo getConstructorInfo = new GetConstructorInfo(); getConstructorInfo.MethodGetActivator(); getConstructorInfo.MethodGetConstructor(); Console.ReadKey(); } } }
(1).在程序运行时,
动态 获取 加载程序集
动态 获取 类型(类,接口)
动态 获取 类型的成员 信息(字段,属性,方法)
(2).在运行时,
动态 创建类型实例,以及 调用 和访问 这些 实例 成员
程序集(Assembly对象)===》类,接口(Type对象)===》类的成员(**Info)
原文:http://www.cnblogs.com/2star/p/5280873.html