Reflection (Reflection) is an important mechanism in. NET, which allows you to obtain members of each type in. NET (including classes, structs, delegates, interfaces, enumerations, and so on) at run time, including methods, properties, events, and constructors. You can also get the name, qualifier, and parameters for each member. With reflection, you can know each type at your fingertips. If you get information about the constructor, you can create the object directly, even if the type of the object is not known at compile time.
The program code builds an executable application after compiling, and we first need to understand the structure of this executable application.
The application architecture is divided into application domains-assemblies-module-type-member tiers, the common language runtime loader manages application domains, which include loading each assembly into the appropriate application domain and controlling the memory layout of the type hierarchy in each assembly.
The assembly contains the module, and the module contains the type, and the type contains the members, and reflection provides an object that encapsulates the assembly, module, and type. We can use reflection to dynamically create instances of types, bind types to existing objects or get types from existing objects, and then call methods of types or access their fields and properties. Reflection usually has the following uses.
(1) Use assembly to define and load assemblies, load lists of modules in the assembly manifest, and find types from this assembly and create instances of that type.
(2) Use module to understand the assembly that contains the module and the classes in the module, and to obtain all the global methods or other specific Non-global methods defined on the module.
(3) Use ConstructorInfo to understand the name, parameters, access modifiers (such as pulic or private) of constructors, and implementation details such as abstract or virtual. Use the GetConstructors or GetConstructor method of type to invoke a specific constructor.
(4) Use MethodInfo to understand the name of the method, return type, parameter, access modifier (such as pulic or private), and implementation details such as abstract or virtual. Use the GetMethods or GetMethod method of type to invoke a particular method.
(5) Use Fiedinfo to understand the name of the field, access modifiers (such as public or private) and implementation details (such as static), and get or set the field value.
(6) Add or remove event handlers by using EventInfo to understand the name of the event, event handler data type, custom attribute, declaring type, and reflection type, and so on.
(7) Get or set the property value by using PropertyInfo to understand the name, data type, declaring type, reflection type, and read-only or writable state of the property.
(8) Use ParameterInfo to understand the name of the parameter, data type, input or output parameters, and the position of the parameter in the method signature.
Classes in System.Reflection.Emit namespaces provide a special form of reflection that can be constructed at run time.
Reflection can also be used to create an application called a type browser that enables users to select a type and then view information about the selected type.
In addition, language compilers such as JScript use reflection to construct symbol tables. Classes in the System.Runtime.Serialization namespace use reflection to access data and determine the fields to be persisted, and the classes in the System.Runtime.Remoting namespace use reflection indirectly by serialization.