The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata...

16
The Metadata System 1 The Metadata System

Transcript of The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata...

Page 1: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 1

The Metadata System

Page 2: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 2

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.

Page 3: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 3

.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

Page 4: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 4

Page 5: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 5

.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

Page 6: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 6

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

Page 7: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 7

ILDASM Tool

Page 8: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 8

Reflection ClassesObject

MemberInfo

EventInfo FieldInfo MethodBase PropertyInfo Type

ConstructorInfo MethodInfo

Page 9: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 9

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

Page 10: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 10

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"); } }}

Page 11: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 11

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.

Page 12: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 12

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)

Page 13: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 13

Assemblies and Manifests (2)

Assembly A

File A Module B

Module A

Manifest

Note: File A is not a module, would not contain any CLR metadata, just hold other resources such as bitmaps.

Page 14: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 14

Assemblies and Manifests (3)

Page 15: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 15

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

Page 16: The Metadata System1. 2 Introduction Metadata is data that describes data. Traditionally, metadata has been found in language- specific files (e.g. C/C++

The Metadata System 16

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.