NET metadata, in the Microsoft .NET framework, refers to code that describes .NET CIL (Common Intermediate Language) code. A .NET language compiler will generate the metadata and store this in the assembly containing the CIL. 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. When the CLR 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.
Attributes
Attributes
Developers 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. A pseudo custom attribute is metadata that the CLR knows about, for example [Serializable] (which means that an instance of the class can be serialized). The 'pseudo' in pseudo custom attribute refers to the fact that the compiler will not use it to generate custom metadata; instead, it will generate metadata that the CLR knows about.
Example (C#):[Serializable]
public class MyClass
{
...
}
{
...
}
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. For example, the Visual Studio property grid groups together properties of an object that is being viewed using categories; the class developer indicates the category for the object's class by applying the [Category] custom attribute. In this case it is application code — the property grid — that interprets the attribute, not the CLR.
How metadata is stored
How metadata is stored
Assemblies 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 metainfo 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.
Reflection
Reflection is the API used to read .NET 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 will allow you to inspect the descriptions of classes and their members, and it will allow you to execute code. However, version 1.1 will not allow you to get access to the CIL for a method. Version 2.0 of the framework will allow you to obtain the CIL for a method.
Besides the System.Reflection namespace, the following tools are available for reading .NET metadata and parsing IL:
PostSharp
Mono Cecil
PostSharp
Mono Cecil
No comments:
Post a Comment