.NET metadata

.NET metadata

.NET metadata, in the Microsoft .NET framework, refers to certain data structures embedded within the Common Intermediate Language 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 .NET language compiler will generate the metadata and store this in the assembly containing the CIL. 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

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. 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 Attribute class. A custom attribute can be used on any method, property, class or entire assembly with the syntax: ["Attribute name"(optional "parameter", optional "name=value" pairs)] as in:

[Custom] [Custom(1)] [Custom(1, comment="yes")]

Custom attributes are used by the .NET Framework 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 [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:

class CustomAttribute : Attribute{ int ParamNumber = 0; string Comment = "";

public RecentAttribute() { } public RecentAttribute(int num) { paramNumber = num; }

public String comment { set { Comment = value; }

Name of the class is mapped to the attribute name. The Visual C# compiler automatically adds the string "attribute" at the end of any attribute name. Consequently, every class implementing the handling of an attribute must end with this string but usage of this string is optional when using the attribute. 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.

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 Serializable and Obsolete are implemented as pseudo-custom attributes. Pseudo-custom attributes should never be used by ILASM, as it has adequate syntax to describe the metadata.Clarifyme|date=March 2008

Metadata storage

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 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 Tools

Besides the System.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 in native code. Third party tools to retrieve and manipulate metadata include [http://www.postsharp.org/ PostSharp] and [http://www.mono-project.com/Cecil Mono Cecil] can also be used.


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • .Net Remoting — NET Remoting это созданный компанией Microsoft API для межпроцессного взаимодействия. Выпущен в 2002 году вместе с версией 1.0 пакета .NET Framework. Это одна из серии технологий от Microsoft, начатой в 1990 году первой версией OLE для 16 разрядн …   Википедия

  • .NET Framework 3.x — NET Framework 3.0, первоначально называвшийся WinFX интерфейс программирования приложений, который входит в состав операционной системы Windows Vista и Windows Server 2008. Старый интерфейс, Win32 API, будет по прежнему доступен, но из него не бу …   Википедия

  • .NET Framework — This article is about the Microsoft technology. For the Top level domain, see .net. For other uses, see .NET. .NET Framework Developer(s) Microsoft …   Wikipedia

  • .NET assembly — In the Microsoft .NET framework, an assembly is a partially compiled code library for use in deployment, versioning and security. In the Microsoft Windows implementation of .NET, an assembly is a PE (portable executable) file. There are two types …   Wikipedia

  • Net.art — has two definitions:net.art is a group of artists who worked in internet art from 1994. The members are usually referenced as Vuk Ćosić, Jodi.org, Alexei Shulgin, Olia Lialina, Heath Bunting. This group was united as a parody of avantgarde… …   Wikipedia

  • net.art — refers to a group of artists who worked in the medium of Internet art from 1994. The main members of this movement are Vuk Ćosić, Jodi.org, Alexei Shulgin, Olia Lialina, and Heath Bunting (irational.org, Heath Bunting, Rachel Baker, Minerva… …   Wikipedia

  • Net-Centric Enterprise Services — (NCES) is a Department of Defense program, managed by the Defense Information Systems Agency, to develop information technology infrastructure services for future systems used by the United States military. There are nine core enterpise services… …   Wikipedia

  • Metadata — For the page on metadata about Wikipedia, see Wikipedia:Metadata. The term metadata is an ambiguous term which is used for two fundamentally different concepts (types). Although the expression data about data is often used, it does not apply to… …   Wikipedia

  • Metadata — Métadonnée  Pour l’article homonyme, voir Métadonnée (audio).  Une métadonnée (mot composé du préfixe grec meta, indiquant l auto référence ; le mot signifie donc proprement « donnée de/à propos de donnée ») est une… …   Wikipédia en Français

  • Métadata — Métadonnée  Pour l’article homonyme, voir Métadonnée (audio).  Une métadonnée (mot composé du préfixe grec meta, indiquant l auto référence ; le mot signifie donc proprement « donnée de/à propos de donnée ») est une… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”