Reflection (Reflection

Source: Internet
Author: User
Reflection (Reflection) is. NET, which can be obtained at runtime through radiation. NET, including methods, attributes, events, and constructor. You can also obtain the name, qualifier, and parameters of each member. With reflection, you can be familiar with every type. If you obtain information about the constructor, you can directly create an object, even if the object type is unknown during compilation.

The program code is compiled to generate an executable application. First, we need to understand the structure of this executable application.

The application structure can be divided into application domain-assembly-module-type-member layers, and the common language runtime loader manages the application domain, 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 provides encapsulated assembly, module, and Type objects (Type ). You 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 call its method or access its fields and properties. If attributes are used in the code, you can use reflection to access them. For more information, see properties.

The following is a static methodGetType-- FromObjectAll types derived from the base class inherit this method-get a simple reflection example of the variable type:

C #

// Using GetType to obtain type information:
Int I = 42;
System. Type type = I. GetType ();
System. Console. WriteLine (type );

Output:

System. Int32

This example uses reflection to obtain the complete name of the loaded assembly:

C #
// Using Reflection to get information from an Assembly:System.Reflection.Assembly o = System.Reflection.Assembly.Load("mscorlib.dll");System.Console.WriteLine(o.GetName());

Output:

Mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089

Reflection Overview

Reflection is useful in the following scenarios:

  • You need to access the properties of the program metadata. For more information, see use reflection access attributes in the topic.

  • Check and instantiate the types in the Assembly.

  • Create a new type at runtime. Use the class in System. Reflection. Emit.

  • Perform post-binding to access the type method created at runtime. See the topic dynamic loading and usage types.

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.