一、開發環境 操作系統:Win7 編譯器:VS2010 .net版本:.net4.0 二、項目結構 三、開發流程 0.編寫實體類 namespace ReflectDemo { public class Bird { public string _id; public string Name { g...
一、開發環境
操作系統: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).在程式運行時,
動態 獲取 載入程式集
動態 獲取 類型(類,介面)
動態 獲取 類型的成員 信息(欄位,屬性,方法)
(2).在運行時,
動態 創建類型實例,以及 調用 和訪問 這些 實例 成員
程式集(Assembly對象)===》類,介面(Type對象)===》類的成員(**Info)