C# Metadata, Metadata Tables and Benefits

    1 Votes

In.NET, metadata is binary information which describes our program that is stored either in common language runtime portable executable file or in memory. When you will compile the code into a portable executable file, metadata is inserted in one portion of the file and the code is converted into Microsoft Intermediate Language (MSIL) and then inserted into another portion of the file.

Every data type and member that is defined and referenced in an assembly is described within metadata. When the code is executed the runtime loads the metadata in memory and referenced it to know the information about the class, members, inheritance, etc. of our class. The portion of metadata in file contains the series of table and heap data structures.

The MSIL portion contains the MSIL and metadata token which refer the metadata portion of the PE file. The metadata contains the following information:
  • Description of assembly
    • Identity (name, version, culture, public key).
    • Types that are exported.
    • Other assemblies where this assembly depends on.
    • The security permissions required to run.
  • Description of types
    • Name, visibility, base class and interfaces implemented
    • Members (methods, fields, properties, events and nested types)
  • Attributes
    • The additional description of the elements that modify types and members.


Benefits of Metadata

Metadata is a key to simple programming model and it eliminates the need for Interface Definition Language (IDL) files, header files or any other external method of the component reference. The major benefits provided by metadata are:

  • Self-describing files: In CLR modules and assemblies are self-describing. The module’s metadata consists of everything required to communicate with another module.
  • Language interoperability: The metadata provides language interoperability and easier component based design. It also provides all the information required of the compiled code to inherit a class from a PE file which is written in different language.
  • Attributes: .Net Framework allows declaring specific kind of metadata which is known as attributes in the compiled file.

Metadata and Portable Executable File Structure

The metadata is stored in one section of .Net Framework PE file while MSIL is stored in another portion of the PE file. The metadata of the file consists of table and heap data structures. The MSIL consists of the MSIL and metadata tokens which refer the metadata portion of the PE file.

Metadata tables and heaps

Each of the metadata table holds the information about the elements of the program. For example one metadata table describes the classes in our code, the second table will describe the fields and so on. If there are ten classes in our code then class table will have ten rows one for each class.

The metadata table reference other tables and heaps. The metadata also stores the information in four heap structures such as string, blob, user string and GUID.

Metadata Tokens:

Every row of each metadata table is uniquely identified in the MSIL portion of the PE file by metadata token. Metadata tokens concepts are similar to pointers, persisted in MSIL which refer a particular metadata table. The token is four byte number. The first byte refers to denote the metadata table which refer a particular token (method, type, etc.). The other three remaining bytes specify the row in the metadata table.

Metadata within a PE file:

When the program is compiled for the CLR then it is converted into PE file which consists of three parts. Here is the table given below:

PE sectionContents
PE header This is the index of the PE's file main section and the address of the entry point.
MSIL instructions MSIL makes up our code and the instructions are accompanied by metadata tokens.
Metadata This is section used by the runtime to record the information about every type and member in the given code.

Use of Metadata at Runtime

Let us understand metadata better and its role in CLR. Let us understand this through an example that how it affects metadata run time life.

using System; 
public class Program
public static int Main ()
int a = 5;
int b = 2;
Console.WriteLine ("The Value is: {0}", Mul (a, b));
return 0;
public static int Mul (int x, int y)
return (x * y);

When the above code will run the runtime will load the module in memory and consult the metadata for this class. As and when the program is loaded, the runtime will perform the analysis of the method’s MSIL to convert it to the native machine. The runtime uses just in time (JIT) compiler to convert the MSIL instructions to native machine code. The MSIL of the above code will be

.maxstack 3
.locals ([0] int32 a,
[1] int32 b,
[2] int32 V_2,
[3] int32 V_3)
IL_0000: ldc.i4.s 5
IL_0002: stloc.0
IL_0003: ldc.i4.s 2
IL_0005: stloc.1
IL_0006: ldstr "The Value is: {0}"
IL_000b: ldloc.0
IL_000c: ldloc.1
IL_000d: call int32 ConsoleApplication.Program:: Mul (int32,int32) /* 06000003 */