Reflection-retrieve information Graph

Source: Internet
Author: User

Use and Analysis of reflection mechanisms in. net
[Date: 2008-06-30] Source: Author: Zhiwei  
  . Net reflection Definition: Ability to review metadata and collect information about its type. Metadata is a binary information used to describe a portable executable file (PE) file stored in a Common Language Runtime library or a program stored in memory. When your code is compiled into a PE file, the metadata is inserted into a part of the file. Convert the code to Microsoft intermediate language (msil) and insert it into another part of the file. Each type and member defined and referenced in a module or assembly is described in metadata. When the code is executed, the runtime loads the metadata into the memory and references it to find information about the code, such as the class, member, and inheritance.

Metadata describes each type and member defined in the Code in a non-specific language. Metadata stores the following information:

Assembly Description: 1. ID (name, version, region, public key ). 2. Export type. 3. Other Assembly on which the Assembly depends. 4. security permissions required for running. Type Description: 1. Name, visibility, base class, and implemented interface. 2. Members (methods, fields, attributes, events, and nested types ). Attribute: 1. modify other descriptive elements of the type and member. System. the reflection namespace contains several classes that allow you to reflect (PARSE) the Code of these metadata tables and the reflection-related namespaces (we access reflection information through these namespaces): system. reflection. memberinfosystem. reflection. eventinfosystem. reflection. fieldinfosystem. reflection. methodbasesystem. reflection. constructorinfosystem. reflection. methodinfosystem. reflection. propertyinfosystem. type System. reflection. assembly

 

 

. Net 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. Key points of application: 1. Reflection is often used when. Net implements the factory model. Many factory models are used. 2. dynamic reflection binding sacrifices performance. 3. Some metadata information cannot be obtained through reflection. 4. Some reflection types are specifically used for the development and use of CLR development compilers, so you must realize that not all reflection types are suitable for everyone. Reflection of a single assembly 1. Load Method: a highly recommended method. The load method carries an Assembly flag and loads it. Load will cause the CLR to apply the policy to the Assembly. Find the assembly in the global assembly Buffer, 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. Layered Reflection Model

 

. Net reflection Overview:

The application structure is divided into application domain-assembly-module-type-member several layers, the Common Language Runtime Library loader manages the application domain.

These domains form a definite boundary around objects with the same application scope.

Such management includes loading each assembly to the corresponding application domain and controlling the memory layout of the type hierarchy of each set.

The Assembly contains modules, while the module contains types and types, and reflection provides encapsulated assembly, module, and type objects.

We can use reflection to dynamically create instances of the type, bind the type to an existing object or obtain the type from the existing object, and then call the method of the type or access its fields and attributes. Reflection is usually used for the following purposes:

(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.

The system. reflection. emit namespace class provides a special form of reflection that can be constructed at runtime.

Reflection can also be used to create an application called a type browser, allowing you to select a type and view information about the selected type.

In addition, JScript and other language compilers use reflection to construct symbol tables. Classes in the system. runtime. serialization namespace use reflection to access data and determine the fields to be permanently saved. classes in the system. runtime. remoting namespace use reflection indirectly through serialization.

Reflection performance:

When Using Reflection to call a type or trigger method, or when accessing a field or attribute, CLR needs to do more work: Check parameters, check permissions, and so on, so the speed is very slow.

Therefore, do not use reflection for programming. You can use the following methods to write a dynamic Constructor (late binding:

1. inherit from the class. This type is derived from a known basic type during compilation. An instance of this type is generated at runtime, and its reference is placed in a variable of its basic type, then, call the virtual method of the basic type. 2. Implemented through interfaces. At runtime, construct an instance of this type, place its reference to a variable of its interface type, and then call the virtual method defined by this interface. 3. Implemented through delegation. Let this type implement a method. Its name and prototype are consistent with a delegate that is known during compilation. Construct an instance of this type at runtime, then construct the instance of the delegate using the object and name of the method, and then call the method you want through the delegate. This method is more efficient than the previous two methods.

Improve the Performance of reflection: the performance loss of reflection mainly comes from the comparison type, traversal members, and calling members. The comparison type consumes the least time.

Calling members consumes the most time. Therefore, using reflection methods such as dynamic member calls can be minimized to improve application performance. In addition, the reflection performance can be improved by binding the Reflection Method in the later stage and avoiding the amplification effect of the Reflection Method in the loop.

Example: This is a calculator. dll

The methods in calculator. dll are checked and called through reflection:

The following figure shows the running result:

Source: http://www.zhiweinet.com/jiaocheng/2008-06/875.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.