I. Basic Concepts
Reflection: reflection is a process of running database type discovery. Reflection can be used to obtain a list of all types contained in a given Assembly. This list includes methods, fields, attributes, and events defined in a given type. You can also dynamically discover the excuses supported by a group of given classes, method parameters, and other related information such as the base class, namespace, and data list.
Ii. namespace
1. Various types in the system. Reflection namespace
(1) An assembly can be used to load, understand, and manipulate an assembly.
(2) assemblyname can be used to find a large amount of information hidden in the Assembly's identity, such as version information and region information.
(3) eventinfo event information
(4) Information of the fieldinfo Field
(5) Information about the methodinfo Method
(6) parameterinfo parameter information
(7) propertyinfo attributes
(8) memberinfo is an abstract base class that defines public behaviors for eventinfo, fieldinfo, methodinfo, propertyinfo, and other types.
(9) module is used to access a given module with multi-file assembly
2. system. Type class
System. Type supports the following types of members:
(1) is *** is used to check a type of metadata, such as isabstract, isclass, and isvaluetype.
(2) Get *** is used to get a specified project from the type, such as getevent () to get a specified event of the type (eventinfo ). In addition, each of these methods has a singular version and a plural version. For example, if getevent () corresponds to a plural version of getevents (), this method returns a related eventinfo array.
(3) findmembers () returns an array of the memberinfo type based on the query conditions.
(4) GetType () This static method returns a type instance based on a string name
(5) invokemember () binds a specified project to a later stage.
3. Get three methods for a type instance (because type is an abstract class, you cannot directly use the new keyword to create a type object)
(1) Use System. Object. GetType ()
E. g: person Pe = new person (); // --------- defines PE as an object of the person class.
Type T = PE. GetType ();
(2) Use the system. type. GetType () static method. The parameter is a fully qualified name of the type.
E. g: type T = type. GetType ("entity. person ");
This method is overloaded and allows two Boolean parameters to be specified. One is used to control whether an exception is thrown when the current type cannot be found,
The other is used to indicate whether the string is case sensitive.
E. g: type T = type. GetType ("entity. Person", false, true );
Note that the input string does not contain the information of the Assembly where the type is located, and the type is considered to be defined in the currently executed assembly.
To obtain the type metadata of an external private assembly, the string parameter must use the type fully qualified name and the friendly name of the Assembly where the type is located.
E. g: type T = type. GetType ("entity. Person", "entity"); // ------ "entity" is the friendly name of the type assembly.
Nested type: the input string can specify a + tag to indicate a nested type. For example, you want to obtain the type information of an enumeration type city nested in the person class,
E. g: type T = type. GetType ("entity. Person + city ");
(3) Use the typeof operator e. g: type T = typeof (person );
Comparison of the three methods: to use the first method, you must first create an instance.
However, when using the typeof operator, you still need to know the compilation information of the type, and use the system. type. GetType () static method.
You do not need to know the compilation information of the type, so it is the preferred method.
The following is an example. We use the previous knowledge to reflect a type object, including reflecting all its visible fields, methods, attributes, and events. Basic attributes of the reflection type. And list the details of one of the methods.
[Source code] 1 using system;
2 using system. Collections. Generic;
3 using system. LINQ;
4 using system. text;
5 using system. reflection;
6
7 using system. collections; // -------- this namespace must be specified to implement the ienumerable Interface
8
9
10 namespace exercise
11 {
12 public class baseperson // ------------- assume that a base class defines a public method and a private method.
13 {
14 public void basepublic ()
15 {
16}
17
18 private void baseprivate ()
19 {
20}
21
22 };
23
24
25 // The person class implements the ienumerable interface so that the array defined in the class can use foreach Enumeration
26 public class person: baseperson, ienumerable
27 {
28 private string name = "Lin tianhui"; // --- name
29 public int age = 20; // --- age
30
31 array children = NULL; // --- child Array
32
33 person ()
34 {
35}
36
37 person (string a, int B)
38 {
39 name =;
40 age = B;
41}
42
43
44 public string name
45 {
46 get {return name ;}
47 set {}
48}
49
50 public int age
51 {
52 get {return age ;}
53 set {}
54}
55
56 public void addage () // --- Method for auto-increment by one year
57 {
58 age + = 1;
59}
60
61
62 public delegate void personnamehandler (string X );
63 public event personnamehandler onchangename; // ------ defines an event for name change
64
65 public void changename (string NAM) // --- rename Method
66 {
67 name = Nam;
68}
69
70 public void changenameandaddage (string name, int age) // ------ A method with two parameters is used to demonstrate detailed reflection of a specific method
71 {
72 this. Name = Name;
73 This. Age + = age;
74}
75
76 public ienumerator getenumerator () // --- implementation Interface
77 {
78 return children. getenumerator ();
79}
80
81}
82
83
84
85 public class Program
86 {
87
88
89 build a custom metadata viewer # region build a custom metadata Viewer
90
91 // ------------- display the method name of the input type
92 public static void listmethods (type T)
93 {
94 console. writeline ("\ n all methods of this type :");
95 methodinfo [] MI = T. getmethods ();
96 foreach (methodinfo m in MI)
97 {
98 console. writeline ("\ t Method Name: {0}", M. Name );
99}
100}
101
102
103 // ------------- display all parameters of a Method
104 public static void listparameters (methodinfo m)
105 {
106 parameterinfo [] Pi = M. getparameters ();
107 foreach (parameterinfo P in PI)
108 {
109 console. writeline ("parameter name: {0} \ t parameter type: {1}", p. Name, P. parametertype );
110}
111}
112
113
114 // ------------- display details of a specific method
115 public static void listmethoddetail (type T, string methodname)
116 {
117 methodinfo M = T. getmethod (methodname );
118 console. writeline ("\ n display method details \ n method name: {0}", methodname );
119 console. writeline ("method return value type: {0}", M. returntype. fullname );
120 listparameters (m );
121
122}
123
124 // ------------ display the field names of the input type
125 public static void listfields (type T)
126 {
127 console. writeline ("\ n all fields of this type :");
128 fieldinfo [] Fi = T. getfields ();
129 foreach (fieldinfo F in FI)
130 {
131 console. writeline ("\ t field name: {0}", F. Name );
132}
133}
134
135 // ------------ display the attribute names of the input type
136 public static void listproperties (type T)
137 {
138 console. writeline ("\ n all properties of this type :");
139 propertyinfo [] Pi = T. getproperties ();
140 foreach (propertyinfo P in PI)
141 {
142 console. writeline ("\ t property name: {0}", p. Name );
143}
144}
145
146
147 // ------------ display the base class name of the input type
148 public static void listinterfaces (type T)
149 {
150 console. writeline ("\ n the interface implemented by this type :");
151 type [] II = T. getinterfaces (); // ----------- the call to getinterfaces () returns an array of the type (indicating that the interface is also a type)
152 foreach (type P in II)
153 {
154 console. writeline ("\ t Interface Name: {0}", p. Name );
155}
156}
157
158 // ------------ display the attribute names of the input type
159 public static void listeven TS (type T)
160 {
161 console. writeline ("\ n names of all events of this type :");
162 eventinfo [] Ei = T. getevents ();
163 foreach (eventinfo e in EI)
164 {
165 console. writeline ("\ n event name: {0}", E. Name );
166}
167}
168
169 // ------------ Other information
170 public static void listotherinfo (type T)
171 {
172 console. writeline ("base class name: {0}", T. basetype );
173 console. writeline ("base class name of the base class: {0}", T. basetype. basetype );
174 console. writeline ("Is it a class? : {0} ", T. isclass );
175 console. writeline ("is it an abstract class? : {0} ", T. isabstract );
176}
177
178
179 # endregion
180
181 public static void main (string [] ARGs)
182 {
183
184 console. writeline ("----------------------------------------------------------------");
185
186 type T = type. GetType ("exercise. person"); // The fully qualified name of the ------- person class is "exercise. Person"
187
188 listotherinfo (t); // reflect other information
189 listfields (t); // reflection Field
190 listproperties (t); // reflection attribute
191 listinterfaces (t); // reflection interface
192 listeven TS (t); // reflection event
193 listmethoddetail (T, "changenameandaddage"); // reflect the details of a specific method
194 listmethods (t); // Reflection Method
195
196
197 console. Readline ();
198}
199}
200}
201
202