C # practical reflection technical notes

Source: Internet
Author: User

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.

The Assembly contains modules, while the module contains types and types including members. Reflection provides encapsulated assembly, module, and type objects. 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. Then, you can call methods of the type or access its fields and attributes. Reflection is usually used for the following purposes:

  • Use Assembly to define and load an Assembly, load the modules listed in the Assembly list, locate the type in this Assembly, and create instances of this type.

  • Use Module to learn the following similar information: including the Module assembly and classes in the Module. You can also obtain all global methods defined on the module or other specific non-Global methods.

  • Use ConstructorInfo to learn the following information: name, parameter, and access modifier of the constructor (for examplePublicOrPrivate) And implementation details (suchAbstractOrVirtual. Use the GetConstructors or GetConstructor method of Type to call a specific constructor.

  • Learn the following information using MethodInfo: method name, return type, parameter, and access modifier (suchPublicOrPrivate) And implementation details (suchAbstractOrVirtual. Use the GetMethods or GetMethod method of Type to call a specific method.

  • Use FieldInfo to learn the following information: field name and access modifier (suchPublicOrPrivate) And implementation details (suchStatic), And obtain or set the field value.

  • Use EventInfo to learn the following similar information: event name, event handler 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 status, and get or set the attribute value.

  • Use ParameterInfo to learn the following similar information: Parameter Name, data type, whether the parameter is an input parameter or an output parameter, and the position of the parameter in the method signature.

  • When you work in only reflection context of an application domain, use CustomAttributeData to learn about custom attributes. UseCustomAttributeDataYou do not have to create Attribute instances to check them.

The System. Reflection. Emit namespace class provides a special form of Reflection, allowing you to construct types at runtime.

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

Reflection has other functions. JScript and other language compilers use reflection to construct symbol tables. The 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 indirectly use Reflection Through serialization.

-- MSDN reflection (C #) Reflection Overview

 

1. dynamically load the Assembly

We can load an assembly by using the Assembly file name.

Assembly libAssembly = Assembly.Load("Lib.Entity");

After an assembly is obtained, we can obtain one or all types of the Assembly.

 

2. Get the type under the Assembly

If we know the full name of a class (including the namespace), we can use the following method to obtain the type of this class:

Type bookType = libAssembly.GetType("Lib.Entity.Book");

Alternatively, we can get all types in this Assembly:

Type[] allTypes = libAssembly.GetTypes();

 

3. dynamically create instances of the type

After obtaining the type, you can create an instance of the type based on the Type:

Lib.Entity.Book book = Activator.CreateInstance(bookType) as Lib.Entity.Book;

In fact, when we know a type, it is unreasonable to dynamically create an instance. I personally think it can be used to instantiate an interface or subclass, which is useful for IOC (control inversion. For example:

IBook bk = Activator.CreateInstance(bookType) as Lib.Entity.Book;

 

4. Call Method

After instantiating this type, you can call this type of method:

MethodInfo method = bookType.GetMethod("GetBookString", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic);output(method.Invoke(book, null).ToString());

 

By using the GetMethod method and the following binding identifier, you can not only obtain public methods, but also obtain private and protected methods and static methods and call them.

 

5. Access fields and attributes

We can get the property information through the GetProperty method of the type:

PropertyInfo info = bookType.GetProperty("Title");

You can also assign values to this attribute:

info.SetValue(book, value, null);

 

Field access is similar.

This access field and attribute technology is often used in ORM.

 

6. Access Attribute

This Attribute is a modifier of the type, method, or field. If yes, we can get it at runtime.

bookType.GetCustomAttributes(false);

This technology works with the previous one and is often used to implement an ORM framework.

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.