This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Metadata" CLI – news ·newspapers ·books ·scholar ·JSTOR(July 2016) (Learn how and when to remove this message) |
| CLI metadata, on-disk representation | |
|---|---|
| Filename extension | .exe,.dll,.winmd |
| Magic number | 0x424A5342 |
| Developed by | Microsoft, Ecma International |
| Standard | ECMA-335 part II |
Metadata, in theCommon Language Infrastructure (CLI), refers to certain data structures embedded within theCommon Intermediate Language (CIL) code that describes the high-level structure of the code. Metadata describes allclasses and class members that are defined in the assembly, and the classes and classmembers 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), thereturn type and all of the methodparameters.
ACLI languagecompiler will generate themetadata and store this in theassembly containing theCIL. 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.
TheWindows Runtime application platform, present inWindows 8 andWindows Phone 8, makes use of the CLI metadata format to describecomponentinterfaces for code written in any of the supportedprogramming languages. A difference in use within theCommon Language Runtime is that an assembly typically does not contain any CIL instructions.[1]
Developers can add metadata to their code throughattributes. There are two types of attributes, custom and pseudo custom attributes, and to the developer these have the samesyntax. 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 regularclass thatinherits from theSystem.Attribute class. A custom attribute can be used on any method, property, class or entire assembly with the syntax:[AttributeName(optionalparameter, optionalname=value pairs)] as in:
[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 asweb services,LINQ to SQL uses them to define the mapping of classes to the underlyingrelational schema,Visual Studio uses them to group togetherproperties of an object, the class developer indicates the category for the object's class by applying the[Category] custom attribute. Custom attributes are interpreted by application code and not the CLR. When the compiler sees a custom attribute it will generate custom metadata that is not recognised by the CLR. The developer has to provide code to read the metadata and act on it. As an example, the attribute shown in the example can be handled by the code:
usingSystem;classCustomAttribute:Attribute{privateintparamNumber=0;privatestringcomment="";publicCustomAttribute(){}publicCustomAttribute(intnum){paramNumber=num;}publicStringComment{set{comment=value;}}}
The name of the class is mapped to the attribute name. TheVisual C# compiler automatically adds the string "Attribute" at the end of any attribute name. Consequently, every attribute class name should end with this string, but it is legal to define an attribute without theAttribute-suffix. When affixing an attribute to an item, the compiler will look for both the literal name and the name withAttribute added to the end, i. e. if you were to write[Custom] the compiler would look for bothCustom andCustomAttribute. If both exist, the compiler fails. The attribute can be prefixed with "@" if you don't want to risk ambiguity, so writing[@Custom] will not matchCustomAttribute. Using the attribute invokes the constructor of the class. Overloaded constructors are supported. Name-Value pairs are mapped to properties, the name denotes the name of the property and the value supplied is set by the property.
Sometimes there is ambiguity concerning to what you are affixing the attribute. Consider the following code:
[Orange]publicintExampleMethod(stringinput){//method body goes here}
What has been marked as orange? Is it theExampleMethod, its return value, or perhaps the entire assembly? In this case, the compiler will default, and treat the attribute as being affixed to the method. If this is not what was intended, or if the author wishes to clarify their code, anattribute target may be specified. Writing[return: Orange] will mark the return value as orange,[assembly: Orange] will mark the entire assembly. The valid targets areassembly,field,event,method,module,param,property,return andtype.
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 asSerializable andObsolete are implemented as pseudo-custom attributes. Pseudo-custom attributes should never be used byILAsm, as it has adequate syntax to describe the metadata.[clarification needed]
Assemblies contain tables of metadata. These tables are described by the CIL specification.[2] 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 FrameworkSDK contains a sample calledmetainfo that will list the metadata tables in an assembly, however, this information is rarely of use to a developer. Metadata in an assembly may be viewed using the ILDASM tool provided by the.NET Framework SDK.
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 thePortable Executable (PE, .exe or .dll) format. The PE form is based on the on-disk form.
Reflection is theAPI 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.
Besides theSystem.Reflection namespace, other tools are also available that can be used to handle metadata. The Microsoft .NET Framework ships a CLR metadata manipulation library that is implemented innative code. Third-party tools to retrieve and manipulate metadata includePostSharp andMono Cecil can also be used.