Net reflection mechanism Overview

Source: Internet
Author: User

1. What is reflection:
Reflection provides an encapsulation.ProgramSet, module, and type object. Through reflection, you can dynamically create instances of the type and bind the type to the current
An object exists, or a type is obtained from an existing object. Then, you can call methods of the type or access its fields and attributes.

 

Ii. Why reflection (advantages of reflection ):

Using Reflection makes it easy for us to use our own or third-party development types to enhance the functionality of our applications and design dynamically scalable applications.

Iii. Typical applications of reflection:

There are few applications that need to use the reflection type. We should also try to avoid using it (performance problems ).

Reflection types are typically used in some class libraries. These class libraries usually need to understand a type definition to provide some rich functions. For example, serialization in FCL
Mechanism, which uses reflection to determine which fields are defined in a type to get their values and write them into byte streams for network transmission (serialization Mechanism
See. Net remoting knowledge ).

In addition, reflection is required when an application needs to load a specific type or set of programs at runtime.

Iv. main purposes of reflection:

1. reflected Assembly (Assembly> module> type member)

Use Assembly to define and load an assembly to obtain information about the assembly;

Instance method: getassemblies ()

2. reflected modules in the Assembly

Use the module to load the modules listed in the assembly list to obtain information about the modules in the Assembly;

Instance method: getmodules ()

3. types in the reflection Module

Use Type to load the type information in the module;

Instance method: gettypes ()

4. Reflection Type members

Use memberinfo to load members and obtain information about members of the type.

Instance method: getmembers ()

Note: The reflected members can include private members. You can use the enumeration type bindingflags as parameters to pass to the overloaded getmembers method. However
Such rights should not be abused.. Net reflection adopts the CAS mechanism (CodeThis is ensured by the access security mechanism. (Enumeration type
For details about bindingflags, see msdn)

The purpose of a reflection type member is:

1) Create an instance of the type

Createinstance method of system. Activator

Createinstancefrom method of system. Activator

Some instance methods of system. appdomain

For example, createinstance, createinstanceandunwrap, createinstancefrom, createinstancefromandunwrap

Invokemember instance method of system. Type

System. reflection. constructorinfo invoke instance method

 

2) call a method of Type

Both binding and calling are executed: The invokemember instance method of system. Type (Note: invokemember does not support events)

Binding and calling separation: (when access is frequent)

The types that can be used to bind members include constructorinfo, methodinfo, fieldinfo, eventinfo, and propertyinfo.

Use constructorinfo to learn the following information: name, parameter, access modifier (such as public or private) and implementation details of the constructorinfo:
(Such as abstract or virtual. Use the getconstructors or getconstructor method of type to call a specific constructor.

Learn the following information using methodinfo: method name, return type, parameter, access modifier (such as public or private), and implementation details
(Such as abstract or virtual. Use the getmethods or getmethod method of type to call a specific method.

Use fieldinfo to learn the following information: field name, access modifier (such as public or private), and implementation details (such as static;
And obtain or set the field value.

Use eventinfo to learn the following similar information: event name, event processing program data type, custom attribute, declaration type, and reflection type;
And add or remove event handlers.

Use propertyinfo to learn the following similar information: attribute name, data type, declaration type, reflection type, read-only or writable state;
And get or set the property value.

5. Why not use reflection (the disadvantage of reflection ):

Using Reflection to call methods, access fields, and attributes may cause performance degradation for four reasons:

1) when searching for Members, binding operations may lead to frequent string comparisons;

2) When passing parameters, you must first construct an array and initialize the elements. Then, when calling the internal method, the reflection mechanism needs to extract parameters from the array,
And put them on the stack;

3) CLR checks the performance overhead caused by the number and type of parameters passed into the method;

4) CLR ensures that the caller has an appropriate security license to access the performance overhead of the member.

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.