160 likes | 303 Views
The Metadata System. Introduction. Metadata is data that describes data. Traditionally, metadata has been found in language-specific files (e.g. C/C++ header files), but providing interoperability between such files across multiple languages is difficult (or almost impossible)
E N D
The Metadata System The Metadata System
Introduction • Metadata is data that describes data. • Traditionally, metadata has been found in language-specific files (e.g. C/C++ header files), but providing interoperability between such files across multiple languages is difficult (or almost impossible) • Compilers usually remove most of the metadata information when source files are compiled, so executable files often have little or no metadata about their types available at runtime • In some systems, components’ metadata is not stored with the components, but as separate files (e.g. IDL in CORBA), may cause versioning and inconsistency problems • Traditional metadata facilities are primitive, allow developers to specify the syntax of an interface but not its semantics • Thus, compilers and execution engine require metadata. The Metadata System
.NET Metadata System • The metadata system is the part of CLR that describes the types in the CLR. • It is the essential facility that enables type sharing between compilers (languages). • Allows metadata to be persisted along with types at compile time. • Can be interrogated by other CLR compilers, and compilers can use the metadata to make types available in their own languages. • Can be used by the execution system at runtime (to manage types) • Enables other languages to inspect, use and extend types The Metadata System
.NET Metadata System • CLR extends the use of metadata to provide not only information about types but also information about assemblies, the unit of deployment in the .NET framework. • Both assemblies and types are self-describing. • Execution engines uses this information to ensure correct runtime execution of code. • Some metadata facilities are needed to access the metadata The Metadata System
Reflection • The act of inspecting a type’s metadata at runtime is called reflection. • System.Object has a method GetType() specifically designed to facilitate this inspection. • GetType() returns an object of type Type. • Listing 3.1 • ILDASM: a tool for examining metadata The Metadata System
ILDASM Tool The Metadata System
Object MemberInfo EventInfo FieldInfo MethodBase PropertyInfo Type ConstructorInfo MethodInfo Reflection Classes The Metadata System
Metadata Extensiblity • To provide more semantic information • By Attributes, essentially a class, an instance of an attribute class is attached to a type or its members and persisted in the metadata for that type • Custom Attributes: simple and easy means for developers to annotate a type or its members • Standard Attributes: Framework Class Library predefined standard attributes The Metadata System
Using a standard attribute(Listing 3.5) using System; namespace Sample { public class TestClass { [Obsolete("Use NewMthod instead of OldMethod")] public static void OldMethod() { Console.WriteLine("Hi World"); } public static void NewMethod() { Console.WriteLine("Hello World"); } } } The Metadata System
Dynamic Discovery of Types • Becoming increasingly important in modern software architectures • Traditionally programming languages and systems used to have such information to be known at compile time, more type-safe (type checking) and efficient • But leads to platform-specific code that is very difficult to migrate and execute on different systems • Even, types can be known only at runtime • .NET framework provides dynamic type discovery through its Reflection facilities. The Metadata System
Assemblies and Manifests (1) • Assembly is basically a collection of files containing types and methods that can be used (similar to DLL libraries) • Can be viewed as a collection of files that form a functionally unit, contain types, resources available to other assemblies. • An assembly houses the components, but also provides facilities such as version information and type resolution. • An assembly comprises one or more modules, a module is a file that contains CLR metadata. • One of the modules has a manifest. The manifest contains information about the assembly, such as its name, version information, and names of all other modules and files • Using an assembly (Listing 3.8) The Metadata System
Assembly A Module A File A Module B Manifest Assemblies and Manifests (2) Note: File A is not a module, would not contain any CLR metadata, just hold other resources such as bitmaps. The Metadata System
Assemblies and Manifests (3) The Metadata System
Assemblies and Manifests (3) .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 1:0:3300:0 } .assembly AssemblyInformation { // --- The following custom attribute is added automatically, do not uncomment ------- // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(bool, // bool) = ( 01 00 01 01 00 00 ) .hash algorithm 0x00008004 .ver 0:0:0:0 } .module AssemblyInformation.exe // MVID: {ADDEC506-DB9F-4E90-80D5-5712F39A3081} .imagebase 0x00400000 .subsystem 0x00000003 .file alignment 512 .corflags 0x00000001 // Image base: 0x01160000 The Metadata System
Meta-Programming • The ability to dynamically create new types at runtime is known as meta-programming. • Supported by CLR’s Reflection.Emit • Creating dynamic types is a fundamental facility in the remoting services, automatic generation of proxy objects at runtime. The Metadata System