. NET reflection, entrust technology and design pattern

Source: Internet
Author: User
Tags abstract constructor modifiers reflection serialization

1, reflective technology and design mode

Reflection (Reflection) is an important mechanism in. NET, which can be obtained at run time by reflection. NET is a member of each type (including classes, structs, delegates, interfaces, enumerations, and so on), 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.

1.1,. NET Executable Application Architecture

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.

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.