Senior C # reflection (Reflection)

Today we chat and reflection problem in c #, and some of his advantages and disadvantages.

These are some of the theoretical knowledge own collection in a number of study materials, we hope to add.

Reflecting means can access the program, the ability to detect and modify its own state or behavior.
Assembly contains the module, which contains the type, type and contain members. Reflecting the object provides a package assembly, and types of modules. Can be created dynamically using the reflection type of instance, bind to an existing object type, or acquired from an existing object type. You can then call the type of approach or access its fields and properties.
Procedures and types of data referred to as metadata (metadata), which program stored in the program set.
Program is running, or the assembly can view other metadata itself. View a program run itself metadata or metadata behavior of other programs is called reflection (reflection).

Use reflection (the Reflection) is
1. It allows (attribute) information View properties at runtime.
2. It allows the collection of various types of review, and instantiating these types.
3. It allows late binding of the methods and properties (property).
4. It allows the creation of a new type at run time, and then using these types perform some tasks.

Some members of the class type:
Here Insert Picture Description

And reflecting acquisition method performed:
1. Obtain type information type = my.GetType the Type ();
2. Create Object Activator.CreateInstance (type) according to the type;
3. The method of acquiring information type.GetMethod ( "MyFun");
. 4. Some flag BindingFlags.Public calling methods
5. execution method method.Invoke

Get reflective properties:
1. The type information acquiring type = my.GetType the Type ();
2. Create Object Activator.CreateInstance (type) according to the type;
3. Access attribute name type.GetProperty ( "Number The")
4. get property values SetValue (dObj, 100, null)

Reflector assembly
Assembly.LoadFile: The method specified file may be loaded from the assembly, the assembly by looking AssemblyRef metadata table, and that all references assemblies required
and Assembly.LoadFrom: loader assembly from a specified file, and the LoadFrom it () except that in the LoadFile () does not load other assemblies referenced by the target assembly and dependency. It needs its own control and display load all dependent assemblies

Binding will establish a link between the use of the class (instance of the class and use instances of classes) assembly Assembly and the class definition: This contact is a correspondence between the assembly method of the class using the class in the class definition invoking assemblies physical address. By means of this contact, the class is going to continue to call a thread.

Early binding : created by the compiler during the creation of an assembly according to the .Net source code. A direct reference equivalent class declaration and instantiate.

Late Binding : use class assembly and no reference type defined class assembly at compile time.
Binding is rather than compile time during code execution. : IDE lose verification of grammar

Custom assemblies : the smallest unit .NET application executed e.g. .dll, .exe

Advantages :
1, reflecting the increase of flexibility and scalability procedures.
2, reduce the coupling, improved adaptability.
3, which allows the program to create and control any class of objects, without having to hard-code ahead of the target class.

Disadvantages :
1, performance problems: use of a substantially reflective explaining the operation, is much slower than for the direct access codes when the fields and methods. Therefore mainly used in reflection of the flexibility and scalability demanding framework, the general procedure is not recommended.
2, the use of internal reflections in the fuzzy programming logic; logic programmer wants to see in the program source code, but the reflection technique bypasses the source code, and thus cause problems maintenance reflection code more complex than the corresponding code directly .

Guess you like

Origin blog.csdn.net/weixin_44370124/article/details/90645493