.NET Framework Introduction: Metadata Ketan Bibave Overview • What is Metadata? • How is Metadata? • Structure of Metadata Tables • Functions or benefits of Metadata What is Metadata? – The .NET Framework makes component interoperation even easier by allowing compilers to emit additional declarative information into all modules and assemblies. This information, called metadata, helps components to interact seamlessly. – It also known as “self-contained” type library. – Metadata is binary information describing your program that is stored in Managed module. – Every type and member that is defined and referenced in a module or assembly is described within metadata. How is Metadata? • Metadata stores the following information: – Description of the assembly. • • • • Identity (name, version, culture, public key). The types that are exported. Other assemblies that this assembly depends on. Security permissions needed to run. – Description of types. • Name, visibility, base class, and interfaces implemented. • Members (methods, fields, properties, events, nested types). – Attributes. • Additional descriptive elements that modify types and members. • All Metadata in the form of tables and all table internally in the forms of hexadecimal numbers. • Metadata is of two type – DefType (Defined) – RefType (Referenced) • There are only 15 tables divided into above two types of tables. How is Metadata? • Metadata Token – A metadata token is a four-byte number. – The top byte denotes the metadata table to which a particular token refers (method, type, and so on). The remaining three bytes specify the row in the metadata table that corresponds to the programming element being described. – If we define a method in C# and compile it into a PE file, the following metadata token might exist in the MSIL portion of the PE file • 0x06000004 – The top byte (0x06) indicates that this is a MethodDef token. The lower three bytes (000004) tells the common language runtime to look in the fourth row of the MethodDef table for the information that describes this method definition – Metadata also stores information in four heap structures: » string, blob, user string, and GUID. » All the strings used to name types and members are stored in the string heap. » For example, a method table does not directly store the name of a particular method, but points to the method's name stored in the string heap. Structure of Metadata Tables 15 Type of Table 11 Defined Type 7 Module Type 4 Reference Type 4 Assembly Type Structure of Metadata Tables: 11 Defined Type Table Module Type ModuleDef 0x00 TypeDef 0x02 FieldDef 0x04 Assembly Type MethodDef 0x06 ParamDef 0x08 PropertyDef 0x17 EventDef 0x14 AssemblyDef 0x20 FileDef 0x26 ExportedTypeDef0x27 ManifestResourceDef 0x28 Structure of Metadata Tables: 4 Reference Type Table Reference Type AssemblyRef 0x23 ModuleRef 0x1 TypeRef 0x01 MemberRef 0x0a Functions or benefits of Metadata • Metadata allows CLR to do Type Safe Verification and hence metadata makes AppDomain possible. • CLR uses metadata for following operations – For object lifetime maintenance and therefore metadata is responsible to automatic garbage collection. – For serialization, deserialization, marshalling and Remote method Invocation using Network data transfer – CLR does Introspection and Reflection. • IDE uses metadata for intellisense feature. • Metadata eliminates the need for Interface Definition Language (IDL) files, header files, or any external method of component reference. • Metadata enables .NET Framework languages to describe themselves automatically in a language-neutral manner Functions or benefits of Metadata • Self-describing files – Common language runtime modules and assemblies are selfdescribing. – Metadata automatically provides the functionality of IDL in COM, so we can use one file for both definition and implementation. • Language interoperability and easier component-based design. – We can create an instance of any class written in any managed language without worrying about explicit marshalling or using custom interoperability code. • Attributes – Attributes are used to control in more detail how your program behaves at run time. – We can emit our own custom metadata into .NET Framework files through user-defined custom attributes. External References • • • • Gokhale Sir Notes. Metadata and Self-Describing Components Metadata and the PE File Structure Run-Time Use of Metadata