C # knowledge point-reflection,
I. Development Environment
Operating System: Win7
Compiler: VS2010
. Net version:. net4.0
Ii. Project Structure
Namespace ReflectDemo {public class Bird {public string _ id; public string Name {get; set;} public int Age {get; set;} public void Eat () {Console. writeLine ("I am a fool");} public void Eat (string birdName) {Console. writeLine ("both me and" + birdName + "are commodities");} public Bird () {} public Bird (string name, int age) {this. name = name; this. age = age;} public void BirdIntroducion () {Console. writeLine ("My Name is" + Name + ", I" + Age + "years old ");}}}1. Get the Assembly object
Namespace ReflectDemo {public class GetAssembly {public void MethodGetAllAssembly () {// obtain the Assembly [] assemblies = AppDomain in the current application domain. currentDomain. getAssemblies (); Console. writeLine (assemblies. toString ();} public void MethodGetCurrentObjAssembly () {// obtain the Assembly assembly of the current object = this. getType (). assembly; Console. writeLine (assembly. toString ();} public void MethodGetFromBin () {// obtain the specified Assembly assembly = Assembly in the bin directory. loadFrom ("ReflectDemo.exe"); Console. writeLine (assembly. toString ());}}}
2. Get the Type object
Namespace ReflectDemo {public class GetType {public void MethodGetByClassName () {// obtain Type type = typeof (Bird) by class name; Console. writeLine (type. toString ();} public void MethodGetByObjName () {// obtain Type Bird bird = new Bird () by Object Name; Type type = bird. getType (); Console. writeLine (type. toString ();} public void MethodGetByFullName () {// namespace. class Name to obtain Assembly assembly = this. getType (). assembly; Type type = assembly. getType ("ReflectDemo. bird ");} public void MethodGetAll () {// obtain all types of Assembly assembly = this. getType (). assembly; Type [] types = assembly. getTypes ();} public void MethodGetAllPublic () {// obtain all the public classes defined in Assembly assembly Assembly = this. getType (). assembly; Type [] types = assembly. getExportedTypes ();}}}
3. Obtain the Type member object 3.1 and obtain the field information
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 get attribute information
Namespace ReflectDemo {public class GetPropertyInfo {public void MethodGetAllPublic () {Bird bird = new Bird () {Name = "yellow"}; Type type = bird. getType (); PropertyInfo nameInfo = type. getProperty ("Name"); string name = nameInfo. getValue (bird, null ). toString (); Console. writeLine (name); nameInfo. setValue (bird, "yellow", null); Console. writeLine (bird. name );}}}
3.3 obtain method information
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 get Constructor
Namespace ReflectDemo {public class GetConstructorInfo {public void MethodGetActivator () {Type type = typeof (Bird); 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. Write console programs
Namespace ReflectDemo {class Program {static void Main (string [] args) {Console. writeLine ("--- get Assembly ---"); GetAssembly getAssembly = new GetAssembly (); getAssembly. methodGetAllAssembly (); getAssembly. methodGetCurrentObjAssembly (); getAssembly. methodGetFromBin (); Console. writeLine ("\ n --- get Type object ---"); GetType getType = new GetType (); getType. methodGetByClassName (); getType. methodGetByObjName (); getType. methodGetByFullName (); getType. methodGetAll (); getType. methodGetAllPublic (); Console. writeLine ("\ n --- get field information ---"); GetFieldInfo getFieldInfo = new GetFieldInfo (); getFieldInfo. methodGetPublicField (); Console. writeLine ("\ n --- get property information ---"); GetPropertyInfo getPropertyInfo = new GetPropertyInfo (); getPropertyInfo. methodGetAllPublic (); Console. writeLine ("\ n --- get method information ---"); GetMethodInfo getMethodInfo = new GetMethodInfo (); getMethodInfo. methodGetWithNoParas (); getMethodInfo. methodWithParas (); Console. writeLine ("\ n --- get the constructor information ---"); GetConstructorInfo getConstructorInfo = new GetConstructorInfo (); getConstructorInfo. methodGetActivator (); getConstructorInfo. methodGetConstructor (); Console. readKey ();}}}
Iv. Project description 1. What is reflection:
(1). When the program is running,
Dynamic acquisition and Assembly Loading
Dynamic acquisition type (class, interface)
Dynamic acquisition of type member information (fields, attributes, methods)
(2). At runtime,
Dynamically create type instances and call and access these instance members
Assembly (Assembly object) ===" class, interface (Type object) ====" class member (** Info)
V. Other information