在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
操作系统:Win7 编译器:VS2010 .net版本:.net4.0 二、项目结构三、开发流程0.编写实体类namespace ReflectDemo { public class Bird { public string _id; public string Name { get; set; } public int Age { get; set; } public void Eat() { Console.WriteLine("我是个吃货"); } public void Eat(string birdName) { Console.WriteLine("我和" + birdName + "都是个吃货"); } public Bird() { } public Bird(string name, int age) { this.Name = name; this.Age = age; } public void BirdIntroducion() { Console.WriteLine("我叫" + Name + ",我" + Age + "岁了"); } } } 1.获取Assembly对象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()); } } } 2.获取Type对象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(); } } } 3.获取Type成员对象3.1获取字段信息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); } } } 3.2获取属性信息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); } } } 3.3获取方法信息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[] { "小黑" }); } } } 3.4获取构造函数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(); } } } 4.编写控制台程序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.什么是反射:(1).在程序运行时,
程序集(Assembly对象)===》类,接口(Type对象)===》类的成员(**Info) 五、其他信息 |
请发表评论