Figure 5 of the DOTNET framework: system. Reflection

Source: Internet
Author: User
Tags dotnet
( Disclaimer: this series is intended for you only. the Net Framework is not an explanation of syntax and class usage. Therefore, you can only briefly describe the mentioned classes. If you have any questions, go to msdn to check )

 

This articleArticleLet's learn the core class of reflection in system. Reflection and. net. Let's first look at the figure:

 

As mentioned in the previous article, the metadata of. Net can fully describe the type, so the Type format can be found at runtime through system. reflection. Let's take a look at the metadata:

 

Source code:

 

  Class Class2
{

Private   Int X;
Private   Int Y;
Public Class2 ( Int A, Int B)
{
X=A;
Y=B;
}
Public   Int Add ()
{
ReturnX+Y;
}
}

 

 

For metadata, seeCodeNote

 

Typedef # 2 ( 02000003 )
// -------------------------------------------------------
// Typdefname: class2 (02000003)
// Flags: [nestedprivate] [autolayout] [Class] [ansiclass] [beforefieldinit] (00100003)
// Extends: 01000002 [typeref] system. Object
// Enclosingclass: Test. class1 (02000002)
// Field #1 (04000004)
// -------------------------------------------------------
// Field name: X (04000004) // /Field name
// Flags: [private] (00000001) // Access Modification
// Callcnvntn: [field]
// Field Type: I4 // Field Type
//  
// Field #2 (04000005)
// -------------------------------------------------------
// Field name: Y (04000005)
// Flags: [private] (00000001)
// Callcnvntn: [field]
// Field Type: I4
//  
// Method #1 (06000004)
// -------------------------------------------------------
// Methodname:. ctor (06000004) // /Constructor
// Flags: [public] [hidebysig] [reuseslot] [specialname] [rtspecialname] [. ctor] (00001886) // Access Modification
// RVA: 0x00002083
// Implflags: [il] [managed] (00000000) // CLR managed code
// Callcnvntn: [Default]
// Hasthis
// Returntype: void // Return type
// 2 arguments
// Argument #1: I4
// Argument #2: I4
// 2 parameters // Parameter List
// (1) paramtoken: (08000003) Name: A flags: [none] (00000000) // Parameter type
// (2) paramtoken: (08000004) Name: B flags: [none] (00000000)
//  
// Method #2 (06000005)
// -------------------------------------------------------
// Methodname: add (06000005) method name
// Flags: [public] [hidebysig] [reuseslot] (00000086) // Access Modification
// RVA: 0x00002099
// Implflags: [il] [managed] (00000000)
// Callcnvntn: [Default]
// Hasthis
// Returntype: I4 // Return type
// No arguments.
//  

 

We can see that in the metadata type, we have defined in detail any member of a class, includingProgramInformation of other Assembly referenced by the set

 

Assemblyref # 1 ( 23000001 )
// -------------------------------------------------------
// Token: 0x23000001
// Public Key or token: B7 7A 5C 56 19 34 E0 89
// Name: mscorlib
// Version: 2.0.0.0
// Major version: 0x00000002
// Minor version: 0x00000000
// Build number: 0x00000000
// Revision number: 0x00000000
// Locale: <null>
// Hashvalue BLOB:
// Flags: [none] (00000000)
//  
//  

 

OK. Now we understand the metadata. Let's take a look at the basic functions of the classes shown above ,. the hierarchical assembly of the netframework library class contains modules, modules contain types, types, and Members (methods, attributes, fields, events, etc ).

 

1. Assembly: mainly load and manipulate an assembly.

2. parameterinfo: This class stores the information of the given parameter. That is to say, the method parameter can be operated using this class.

3. Module: access a given module with multi-module Assembly. In the vs environment, only single-Module assembly can be compiled.

4. memberinfo: this class is an abstract base class that defines public behavior for its subclass. such as membertypes and membername. its sub-classes are specific implementation of methods, attributes, fields, events, and other information.

 

How do these classes perform type reflection?

 

Assembly ASM = Assembly. Load ( " Test " ); // DLL name
Type T = ASM. GetType ( " Test. class2 " ); // Class Name
Object OBJ = Activator. createinstance (t ); // Create a class2 instance
Methodinfo Mi = T. getmethod ( " Add " ); // Obtain information about the add method in class2.
String S = ( String ) MI. Invoke (OBJ, Null ); // The execution method requires parameter 1: the instance of class2, and 2: The parameter list of the method.

Fieldinfo pi = T. getfield ( " X " ); // Obtain Field Information
Pi. getvalue (OBJ ); // Obtain the field value.

 

The reflection call of events and properties is similar to the above Code and will not be described in detail. The type is used in the above Code. Here is a brief introduction.

System. type defines a large number of members and can be used to check the metadata of a type, such as getmethod and getfields. Basically, all information of a type can be obtained, which is very powerful. haha.

 

Next article: Figure 6 of the DOTNET framework: system. xml

 

 

 

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.