Metadata (CLI)
Metadata, in the Common Language Infrastructure (CLI), refers to certain data structures embedded within the Common Intermediate Language (CIL) code that describes the high-level structure of the code. Metadata describes all classes and class members that are defined in the assembly, and the classes and class members that the current assembly will call from another assembly. The metadata for a method contains the complete description of the method, including the class (and the assembly that contains the class), the return type and all of the method parameters. A CLI language compiler will generate the metadata and store this in the assembly containing the CIL. When the run-time executes CIL it will check to make sure that the metadata of the called method is the same as the metadata that is stored in the calling method. This ensures that a method can only be called with exactly the right number of parameters and exactly the right parameter types. The Windows Runtime application platform, present in Windows 8 and Windows Phone 8, makes use of the CLI metadata format to describe component interfaces for code written in any of the supported programming languages. A difference in use within the Common Language Runtime is that an assembly typically does not contain any CIL instructions.[1] AttributesDevelopers can add metadata to their code through attributes. There are two types of attributes, custom and pseudo custom attributes, and to the developer these have the same syntax. Attributes in code are messages to the compiler to generate metadata. In CIL, metadata such as inheritance modifiers, scope modifiers, and almost anything that isn't either opcodes or streams, are also referred to as attributes. A custom attribute is a regular class that inherits from the [Custom]
[Custom(1)]
[Custom(1, Comment="yes")]
Custom attributes are used by CLI extensively. Windows Communication Framework uses attributes to define service contracts, ASP.NET uses these to expose methods as web services, LINQ to SQL uses them to define the mapping of classes to the underlying relational schema, Visual Studio uses them to group together properties of an object, the class developer indicates the category for the object's class by applying the class CustomAttribute : Attribute
{
private int paramNumber = 0;
private string comment = "";
public CustomAttribute() { }
public CustomAttribute(int num) { paramNumber = num; }
public String Comment
{
set { comment = value; }
}
}
The name of the class is mapped to the attribute name. The Visual C# compiler automatically adds the string " Sometimes there is ambiguity concerning to what you are affixing the attribute. Consider the following code: [Orange]
public int ExampleMethod(string input)
{
//method body goes here
}
What has been marked as orange? Is it the A pseudo-custom attribute is used just like regular custom attributes but they do not have a custom handler; rather the compiler has intrinsic awareness of the attributes and handles the code marked with such attributes differently. Attributes such as Metadata storageAssemblies contain tables of metadata. These tables are described by the CIL specification. The metadata tables will have zero or more entries and the position of an entry determines its index. When CIL code uses metadata it does so through a metadata token. This is a 32-bit value where the top 8 bits identify the appropriate metadata table, and the remaining 24 bits give the index of the metadata in the table. The Framework SDK contains a sample called In the CIL standard, metadata is defined in ILAsm (assembly language) form, an on-disk representation form for storage, and a form that is embedded into assemblies of the Portable Executable (PE, .exe or .dll) format. The PE form is based on the on-disk form. ReflectionReflection is the API used to read CLI metadata. The reflection API provides a logical view of metadata rather than the literal view provided by tools like metainfo. Reflection in version 1.1 of the .NET framework can be used to inspect the descriptions of classes and their members, and invoke methods. However, it does not allow runtime access to the CIL for a method. Version 2.0 of the framework allows the CIL for a method to be obtained. Other metadata toolsBesides the See alsoReferences
|