Reflection definition: the ability to review metadata and collect information about its type. Metadata (the most basic data unit after compilation) is a lot of tables. When compiling an assembly or module, the compiler creates a class definition table and a field definition table, and a method definition table.
The System. reflection namespace contains several classes that allow you to reflect (PARSE) the Code of these metadata tables
System. Reflection. Assembly
System. Reflection. MemberInfo
System. Reflection. EventInfo
System. Reflection. FieldInfo
System. Reflection. MethodBase
System. Reflection. ConstructorInfo
System. Reflection. MethodInfo
System. Reflection. PropertyInfo
System. Type
The usage of the above classes is as follows:
(1) Use Assembly to define and load the Assembly, load the module in the Assembly list, and locate the type in this Assembly and create instances of this type.
(2) Use the Module to understand the Assembly containing the Module and the classes in the Module, and obtain all 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) and implementation details (such as abstract or virtual) of the ConstructorInfo. Use the GetConstructors or GetConstructor method of Type to call a specific constructor.
(4) use MethodInfo to understand the method name, return type, parameters, access modifiers (such as pulic or private), and implementation details (such as abstract or virtual. Use the GetMethods or GetMethod method of Type to call a specific method.
(5) Use FiedInfo to learn the field name, access modifier (such as public or private), implementation details (such as static), and obtain or set the field value.
(6) Use EventInfo to learn about the event name, event handler data type, custom attributes, declaration type, and reflection type, and add or remove event handlers.
(7) Use PropertyInfo to understand the attribute name, data type, declaration type, reflection type, read-only or writable status, and obtain or set the attribute value.
(8) Use ParameterInfo to know the parameter name, data type, input parameter, output parameter, and the parameter location in the method signature.
Layered reflection model:
(Note: There is a one-to-many relationship between layers)
Function of reflection:
1. You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from an existing object.
2. The application needs to load a specific type from a specific program set at runtime, so that reflection can be used for a task.
3. Reflection main applications and class libraries. These class libraries need to know a type definition to provide more functions.
Application highlights:
1. In real applications, few applications need to use the reflection type.
2. dynamic reflection binding sacrifices performance.
3. Some metadata information cannot be obtained through reflection.
4. Some reflection types are specially used for the development and use of clr development compilers, so you must realize that not all reflection types are suitable for everyone.
Assembly that reflects appDomain:
When you need to reflect all the Assembly contained in the AppDomain, the example is as follows:
Static void Main
{
// Call all the assembly of appDomain through GetAssemblies
Foreach (Assembly assem in Appdomain. currentDomain. GetAssemblies ())
{
// Reflect the information of the current Assembly
Reflector. ReflectOnAssembly (assem)
}
}
Note: The GetAssemblies method of the AppDomain object will return an array composed of System. Reflection. Assembly elements.
Reflection of a single assembly:
The above method is used to reflect all the assembly of the AppDomain. We can display the call to one of the assembly. The system. reflecton. assembly type provides the following three methods:
1. Load Method: A strongly recommended method. The Load method carries an Assembly flag and loads it. Load will cause the CLR to apply the policy to the Assembly, successively in the global assembly buffer, find the Assembly under the application base directory and private path. If the Assembly cannot be found, the system throws an exception.
2. LoadFrom method: Pass the path name (including the extension) of an assembly file. CLR loads the specified assembly. The passed parameter cannot contain any information about the version number, culture, and public key information. If the Assembly cannot be found in the specified path, an exception is thrown.
3. LoadWithPartialName: never use this method, because the application cannot determine the version of the loaded assembly. The only purpose of this method is to help customers who use a certain behavior provided by the. Net Framework in the testing stage of the. net Framework. This method will eventually be discarded.
Note: system. appDomain also provides a Load method, which is different from the static Load method of Assembly. The load method of AppDomain is an instance method, and a reference to the Assembly is returned, the static Load of Assembly sends the Assembly value encapsulation back to the AppDomain that sends the call. avoid using the AppDomain load method whenever possible
Use reflection to obtain the type information:
After talking about Assembly reflection, let's talk about the third layer in the reflection hierarchy model, type reflection.
A simple example of retrieving type information using reflection:
Using system;
Using sytem. reflection;
Class reflecting
{
Static void Main (string [] args)
{
Reflecting reflect = new reflecting (); // defines a new class.
// Call a reflecting.exe assembly
Assembly myAssembly assembly.loadfrom(“reflecting.exe ")
Reflect. getreflectioninfo (myAssembly); // gets the reflection Information
}
// Define a method to obtain the reflected content
Void getreflectioninfo (assembly myassembly)
{
Type [] typearr = myassemby. Gettypes (); // obtain the type
Foreach (type in typearr) // obtain detailed information for each type
{
// Obtain the structure information of the type
Constructorinfo [] myconstructors = type. GetConstructors;
// Obtain the field information of the type.
Fieldinfo [] myfields = type. GetFiedls ()
// Obtain method information
MethodInfo myMethodInfo = type. GetMethods ();
// Obtain the Property Information
PropertyInfo [] myproperties = type. GetProperties
// Obtain event information
EventInfo [] Myevents = type. GetEvents;
}
}
}
Other methods for getting type objects:
1. The System. type parameter is of the string type. The full name of the type (including its namespace) must be specified for this string)
2. System. type provides two instance methods: GetNestedType and GetNestedTypes.
3. The instance methods provided by Syetem. Reflection. Assembly are GetType, GetTypes, and GetExporedTypes.
4. System. Reflection. Moudle provides these instance methods: GetType, GetTypes, FindTypes