Understanding of metadata and self-description in C #

Source: Internet
Author: User
Understanding of metadata and self-description in C #

The following information is taken from msdn, which provides a good answer to what metadata includes, how it is organized and managed in metadata, and, why does metadata belong to the "self-description type.

Metadata Overview:

Metadata is a binary information used to describe a portable executable file (PE) file stored in a Common Language Runtime library or a program stored in memory. When your code is compiled into a PE file, the metadata is inserted into a part of the file, and the code is converted to Microsoft intermediate language (msil) and insert it into another part of the file. Each type and member defined and referenced in a module or assembly is described in metadata. When the code is executed, the runtime loads the metadata into the memory and references it to find information about the code, such as the class, member, and inheritance.

Metadata describes each type and member defined in the Code in a non-specific language. Metadata stores the following information:

  • Assembly description.

    • Identifier (name, version, region, and public key ).

    • Export type.

    • Other Assembly on which the Assembly depends.

    • Security permissions required for running.

  • Type description.

    • Name, visibility, base class, and implemented interface.

    • Member (method, field, attribute, event, nested type ).

  • Attribute.

    • Modifier types and other descriptive elements of members.

Advantages of metadata

For a simpler programming model, metadata is the key. This model no longer requires an Interface Definition Language (IDL) file, header file, or any external component reference method. Metadata allows the. NET language to automatically describe itself in a non-specific language, which is invisible to developers and users. In addition, you can use attributes to expand the metadata. Metadata has the following advantages:

  • User description file.

    The modules and assembly of the Common Language Runtime Library are self-described. The metadata of a module contains all the information required for interaction with another module. Metadata automatically provides the IDL function in COM, allowing a file to be defined and implemented at the same time. The runtime module and Assembly do not even need to be registered with the operating system. As a result, the instructions used by the running database always reflect the actual code in the compiled file, thus improving the reliability of the application.

  • Language interoperability and simpler component-based design.

    Metadata provides all required information about compiled code for you to inherit classes from PE files written in different languages. You can create instances of any class written in any hosting language (any language for the public language runtime) without worrying about explicit blocking or using custom mutual code.

  • Attribute.

    . NET framework allows you to declare a specific type of metadata (called attributes) in the compilation file ). The existence of attributes can be found everywhere in the entire. NET Framework. attributes are used to more accurately control how your program works during runtime. In addition, you can send your own custom metadata to the. NET Framework file through user-defined custom attributes. For more information, see

    Metadata and PE file structure:

    Metadata is stored in one part of the. NET Framework portable executable file (PE) file, while Microsoft intermediate language (msil) is stored in another part of the PE file. The metadata part of the file contains a series of table and heap data structures. The msil part contains the metadata tag of the msil and the metadata section of the reference PE file. You may encounter metadata tags when using tools (for example, msil used to view code or memory dump used.

    Metadata table and heap

    Each metadata table retains information about program elements. For example, one metadata table describes the class in the code, and the other metadata table describes fields. If there are 10 classes in your code, the class table will have 10 rows and each row will have one class. Metadata tables reference other tables and heaps. For example, the metadata table of the class references the method table.

    Metadata also stores information in four heap structures: String, blob, user string, and guid. All strings used to name types and members are stored in the String Heap. For example, a method table does not directly store the name of a specific method, but points to the name of the method stored in the String Heap.

    Metadata tag

    The metadata tag uniquely identifies each row of each metadata table in the msil section of the PE file. Metadata tags are similar in concept to pointers and permanently reside in msil to reference specific metadata tables.

    The metadata tag is a four-byte number. The maximum byte indicates the metadata table referenced by a specific tag (method, type, etc. The remaining three bytes specify the rows in the meta data table corresponding to the described programming element. If you use C # To define a method and compile it into a PE file, the following metadata mark may exist in the msil part of the PE file:

    Zero X 06000004

    The maximum byte (0x06) indicates that this isMethoddefMark. The lower three bytes (000004) indicate that the Common Language Runtime Library is
    Methoddef
    The fourth row of the table describes the method definition.

    Metadata in PE files

    When a library compilation program is run for a common language, the program is converted to a PE file consisting of three parts. The following table describes the content of each part.

    PE section

    PE content

    PE Header

    The index of the main part of the PE file and the endpoint address.

    The runtime uses this information to identify the file as a PE file and determine where the execution starts when the program is loaded into the memory.

    Msil commands

    The Microsoft intermediate language instruction (msil) that makes up the code ). Many msil commands carry metadata tags.

    Metadata

    Metadata table and heap. The Runtime library uses this part to record information about each type and member in your code. This section also includes custom attributes and security information.

The role of metadata in runtime:

It may be helpful to better understand metadata and its role in the public Language Runtime Library, construct a simple program, and explain how metadata affects its runtime. The following code example shows two methods in the class named MyApp. The main method is the program entry point, while the add method returns only the sum of two integer parameters.

Using system; public class MyApp {public static int main () {int valueone = 10; int valuetwo = 20; console. writeline ("the value is: {0}", add (valueone, valuetwo); Return 0;} public static int add (INT one, int two) {return (one + two );}}

When the code is running, the Runtime Library loads the module into the memory and asks metadata about the information of this class. After loading, the Runtime Library performs extensive analysis on the Microsoft intermediate language (msil) stream of the method and converts it into a fast local command. The Runtime library uses the real-time (JIT) compiler to convert msil commands to local code as needed. Each time a method is converted.

The following example shows some msil generated from the main function of the previous code. You can view msil and metadata from any. NET Framework application.

. Entrypoint. maxstack 3. locals ([0] int32 valueone, [1] int32 valuetwo, [2] int32 V_2, [3] int32 V_3) il_0000: LDC. i4.s 10 il_0002: stloc.0 il_0003: LDC. i4.s 20 il_0005: stloc.1 il_0006: ldstr "the value is: {0}" il_000b: ldloc.0 il_000c: ldloc.1 il_000d:
Call int32 consoleapplication. MyApp: add (int32, int32)/* 06000003 */

The JIT compiler reads the msil of the entire method, thoroughly analyzes it, and generates valid local commands for the method. Metadata tag of the add method (/* 06000003 */) encountered in il_000d. The runtime uses this tag reference
MethoddefThe third row of the table.

The following table shows the metadata tag referenced by the add method.MethoddefA part of the table. Although other metadata tables exist in the dataset and have their own unique values, we will only discuss this table here.

Row

Relative virtual address (RVA)

Implflags

Flags

Name

(Pointing to the String Heap .)

Signature (pointing to blob heap)

1

Zero X 00002050

Il

Managed

Public

Reuseslot

Specialname

Rtspecialname

. Ctor

. Ctor (constructor)

2

Zero X 00002058

Il

Managed

Public

Static

Reuseslot

Main

String

3

0x0000208c

Il

Managed

Public

Static

Reuseslot

Add

Int, Int, int

Each column in the table contains important information about the code.RVAColumn allows the Runtime Library to calculate the starting memory address of the msil that defines this method.ImplflagsAnd
FlagsThe column contains bitwise blocking (for example, whether the method is public or private ).NameThe column indexes The Name Of The method from the String Heap.SignatureThe column indexes the definition of the method signature in the Blob heap.

InRVACalculate the offset address required by the column and return the address to the JIT compiler. Then, the JIT compiler enters the new address. The JIT compiler continues to process msil at the new address until it encounters another metadata tag and repeats the process.

With metadata, the runtime database can access all the information required to Load Code and process it as a local command. In this way, metadata makes it possible to create a self-describing file, a public-type system, and cross-language inheritance.

From: http://hi.baidu.com/luoyuonline/item/d348601bca180d8d89a95653
Related Article

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.