table of Contents
Reflection overview
Reflection appdomain's assembly
Reflecting a single assembly
Using reflection acquisition type information
Set a member of the reflection type
Instances of creating types by reflection
Reflection type interface
Reflection performance
Reflection overview
Definition of reflection: Review metadata and collect the ability of its type information. Metadata (the most basic data unit after compilation) is a large pile of tables. When compiling assemblies or modules, the compiler creates a class definition table, a field definition table, and a method definition form, etc. System.Reflection namespace contains several classes, allowing you to reflex (parse) the code of these metadata tables
And reflective namespace (we are accessing reflection information through these namespace):
System.Reflection.memberInfo
System.Reflection.EventInfo
System.Reflection.fieldInfo
System.Reflection.Methodbase
System.reflection.constructorinfo
System.reflection.MethodInfo
System.reflection.propertyInfo
System.Type
System.Reflection.assembly
Reflection hierarchical model:
Note: Among the intermodes of relationships
The role of reflection:
1. You can create a type of instance using a reflection, bind the type to an existing object, or get the type from the existing object.
2. The application needs to load a specific type from a particular assembly at runtime to reflect a certain task.
3. Reflecting the main application and class library, these class libraries need to know a type of definition to provide more features.
Application points:
1. There are very few applications that need to be used in real-world applications.
2. Use reflex dynamic binding requires sacrificial performance
3. Some metadata information cannot be obtained by reflection.
4. Some reflection types are specially used for the development of the CLR development compiler, so you have to realize that not all reflection types are suitable for everyone.
Reflection appdomain's assembly
When you need to reflect all the assemblies contained in Appdomain, examples are as follows: Static void main
{
/ / Call all the assemblies of Appdomain via getassemblies
Foreach (askEMBLY Assem in AppDomain.currentDomain.getassembly ())
{
// Reflect information about the current assembly
Reflector.Reflectonassembly (assm)
}
}
Note: The getassemblies method that calls the AppDomain object will return an array that consists of system.Reflection.Assembly elements.
Reflecting a single assembly
The above method is to reflect all the assembly of AppDomain, and we can display an assembly in which the system.reflecton.assembly provides the following three methods:
1. LOAD method: a method of high recommendation, the LOAD method with an assembly flag and load it, LOAD will cause the CLR to apply the policy to the assembly, and then in the global assembly buffer, application base catalog and private path Look for this assembly below, if you can't find the assembly system throw an exception
2. LOADFROM method: Pass the path name (including extensions) of an assembly file, the CLR will load the assembly you specify, the parameter passed cannot contain any information, regionality, and public key information about the version number, if The assembly throw an exception in the specified path cannot be found. 3. LoadwithPartialName: Never use this method because the application cannot determine the version of the assembly that is loaded. The unique use of this method is to help customers who use the .NET framework in the .NET framework, this method will eventually be abandoned.
Note: System.Appdomain also provides a LOAD method, his and Assembly's static LOAD method is different, AppDomain's LOAD method is an instance method, returning a reference to the assembly, assembly's static LOAD party The assembly is sent back to the Appdomain that is sent back to the call. Try to avoid the use of Appdomain's LOAD method
Using reflection acquisition type information
The front is over the reflection of the assembly, and the third level in the reflection hierarchy is told, the type reflection
A simple example of using reflection acquisition type information:
Using system;
Using Sytem.Reflection;
Class Reflecting
{
Static void main (string [] args)
{
Reflecting reflect = new reflecting (); // Define a new self-class
// Call a reflecting.exe assembly
askEMBLY Myassembly = askMBLY.LOADFROM ("Reflecting.exe")
Reflect.getReflectionInfo (Myassembly); // Get reflection information
}
/ / Define a method of getting reflection content
Void getReflectionInfo (askMBLY Myassembly)
{
TYPE [] Typearr = myassemby.gettypes (); // Get Type
Foreach (Type IN Typearr) // Get details for each type
{
// Get the type of structure information
Constructorinfo [] myconstructors = type.getconstructors;
/ / Get the type of field information
FieldInfo [] myfields = type.getfiedls ()
// Get method information
MethodInfo mymethodinfo = type.getMethods ();
// Get attribute information
PropertyInfo [] MyProperties = Type.getProperties
// Get event information
Eventinfo [] myevents = type.getevents;
}
}
}
Several several methods for acquiring Type objects:
1. The System.Type parameter is a string type, which must specify the full name of the type (including its namespace)
2. System.Type provides two instance methods: getNestedType, getNestedTypes
3. The instance method provided by the Syetem.Reflection.Assembly type is: gettype, gettypes, getExporedTypes
4. System.Reflection.moudle provides these instance: gettype, gettypes, findtypes
Set a member of the reflection type
Members of the reflection type are the bottom layer of data in the reflection hierarchy model. We can get a type of member through the GetMembers method of the Type object. If we use a GetMembers without parameters, it only returns a static variable and instance member of this type of public definition, and we can also return to the specified type member by using the parameter-based getMembers. Detailed description of the type of System.Reflection.BindingFlags in MSDN. E.g:
/ / Set the content of the type of type that needs to return
BINDINGFLAGS BF = BingdingFlags.Declaredonly | BINGDINGFLAGS.NONPUBLIC | BINGDINGFLAGS.PUBLIC
Foreach (MemberInfo Mi Int T.getMembers (BF))
{
WriteLine (mi.membertype) // output the specified type member
}
Instances of creating types by reflection
By reflection, you can get the type of assembly, we can create this type of instance based on the acquired assembly type, which is also the function of the presentation of the object to be bonded at the time of running at runtime.
We can implement them by the following methods:
1. System.actiVator's CreateInstance method. This method returns a reference to the new object. See MSND for specific usage
2. System.Activator's CreateInstanceFrom is similar to the previous method, but you need to specify the type and its assemblies.
3. System.Appdomain Method: CreateInstance, CreateInstanceAndunwrap, CreateInstranceFrom, and CreateInstraceFromandUnwrap
4. System.Type's InvokeMember instance method: This method returns a constructor that is consistent with the incoming parameters and constructs this type.
5. System.Reflection.constructInfo's INVOKE instance method
Reflection type interface
If you want to get all the interface collections inherited, you can call Type's FindInterface GetInterface or getInterfaces. All of these methods can only return the interface directly inherited, they don't return the interface that inherits from an interface. The basic interface to return the interface must be called again.
Reflection performance:
Use reflection to call types or trigger methods, or access a field or attribute, the CLR needs to do more: check parameters, check permissions, etc., so speed is very slow. So try not to use reflection to program, for the application that intends to write a dynamic constructed type (evening binder), you can take the following ways to replace:
1. Through the inheritance relationship of the class. Let this type derive from a compile-known basic type, generate an instance of this type of schema at runtime, which will be placed in a variable of its base type, then call the basic type of virtual method.
2. Implemented through the interface. At runtime, a instance of this type is built, and the reference will be placed in a variable of its interface type, then call the imaginary method defined by the interface.
3. By delegate implementation. Let this type implement a method, its name and prototype are compliant with a known entrustion when compiling. The instance of this type is constructed at runtime, and then constructs an instance of the delegate with the object and name of this method, then the method you want by delegate. This method is more than the work made by the previous two methods, the efficiency is lower.