47
Introduction To .NET Metadata Matt Pietrek

DocumentN4

Embed Size (px)

Citation preview

Page 1: DocumentN4

Introduction To .NET Metadata

Matt Pietrek

Page 2: DocumentN4

Housekeeping

Who is this guy?Co-founder, Mindreef LLC (www.mindreef.com)President, Wheaty Productions Inc.MSDN’s “Under the Hood” columnist

web: http://www.wheaty.netemail: [email protected] availability

Page 3: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 4: DocumentN4

Metadata: The Big PictureMetadata is information stored in .NET assemblies that makes them self-describingMetadata describes:

Classes (types) provided by the assemblyMethod and field information for classesVersion informationDependencies on other assemblies and modulesSecurity attributes required

Metadata provides the information that the .NET runtime needs to ensure type safety and security

Page 5: DocumentN4

Demo: Many Views Of Metadata

Page 6: DocumentN4

Metadata: Evolution Of A COM ConceptCOM IDL files describe interfaces, methods, parameters, and types in sufficient detail to marshal calls between processesCOM Type Libraries are just a binary representation of IDL informationMetadata is effectively type libraries on steroidsMicrosoft provides tools to convert between metadata and type libraries

Page 7: DocumentN4

Metadata Usage In .NET

The .NET runtime relies heavily on metadata to link together calls and data references at runtimeMetadata is stored in a binary format, and subject to changeMicrosoft provides .NET classes and classic COM interfaces to read and write metadata

Page 8: DocumentN4

The Advantage Of MetadataA shared standard of representing information between any .NET compatible language

Cool benefit: Intellisense for any .NET componentMetadata is automatically generated and consumed by .NET compilers

No need to create .IDL files to expose your code to othersNo need to mess around with .H files and import libraries

Page 9: DocumentN4

Emitting Metadata

Metadata (and associated IL) can be created using the .NET runtime

Compilers aren’t the only ones that get to play!

System.Reflection.Emit classes are the basis for dynamically creating code:

AssemblyBuilderMethodBuilderILGeneratorMany more!

Focus in this presentation is consuming metadata

Page 10: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 11: DocumentN4

The Metadata Hierarchy I

Assembly(e.g., mscorlib)

Modules

Module(i.e., a DLL)

Classes

Global Methods& Variables

Collections

SingleInstances

Page 12: DocumentN4

The Metadata Hierarchy IIClass

(e.g., System.Object)

Methods

Fields

Properties

Events

Method(e.g., “Foo”)

Parameters

Parameter“int Param1”

Parameter“String Param2”

Page 13: DocumentN4

Assembly Metadata

NamePublic keyVersion # (major & minor)Build #Revision #Dependencies on other assembliesLocale

Page 14: DocumentN4

Class Metadata Contents

NameFlags (CorTypeAttr)

Visibility (Public, private, assembly, …)Layout (auto, sequential, explicit…)Type (class, interface, value type, …)

Base class (if applicable)

Page 15: DocumentN4

Method Metadata Contents

NameParent classFlags (CorMethodAttr)

VisibilityStatic / VirtualPlatform Invoke (interop to legacy code)

Implementation Flags (CorMethodImpl)IL / OptIL / NativeManaged / Unmanaged implementation

SignatureRVA to IL in executable file

Page 16: DocumentN4

Parameter Metadata Contents

NameParent methodOrder in method parameter listAttributes (CorParamAttr)

In / OutHas default valueOptional

Page 17: DocumentN4

Field Metadata Contents

NameParent classAttributes (CorFieldAttr)

VisibilityStatic member

Signature (implicitly gives the size)

Page 18: DocumentN4

Property Metadata Contents

NameParent classAttributes (CorPropertyAttr)Signature (implicitly gives the size)Getter & Setter methodsDefault value

Page 19: DocumentN4

Event Metadata Contents

NameParent classFired methodAddOn / RemoveOn methods

Page 20: DocumentN4

Custom Attributes

Custom attributes are special classes that provide additional information about a:

Assembly / ModuleClass / StructMethod / Constructor / PropertyEnumParameter / Field

Unmanaged and Reflection APIs for reading / creating custom attributes

Page 21: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 22: DocumentN4

About The Reflection API

Full access to metadata using .NET runtime classesAssemblies, classes, methods, fields, etc… are all represented as .NET objectsLayered over the unmanaged metadata interfaces

Page 23: DocumentN4

The Metadata Hierarchy As Seen By The Reflection API

ManifestResource

FieldInfo PropertyInfo

ParameterInfo

MethodInfo EventInfo

Type

Module AssemblyName

Assembly

CustomAttributes

Page 24: DocumentN4

Getting To The Reflection API

There are a variety of entry points into the Reflection classesAssembly::LoadFrom loads a file based assembly

Returns an Assembly *For an existing .NET object, use the ::GetType method to get a Type*

Type::Module property moves up the hierarchyType::GetMethods, etc… moves down

Page 25: DocumentN4

Demo

ReflectCS program

Page 26: DocumentN4

Example: Enumerating All Methods In An Assembly

Use Assembly::LoadFrom to load a .NET assemblyReturns an Assembly *

Assembly::GetModules returns an array of Module *’sFor each Module *, call Module::GetTypes

Returns an array of Type *’s

For each Type *, call Type::GetMethodsReturns an array of MethodInfo *’s

For each MethodInfo *, the MethodInfo::Nameproperty returns the method’s name

Page 27: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 28: DocumentN4

About The Unmanaged API

Regular COM interfaces that don’t use the .NET runtimeRelies heavily on “tokens”More granular than equivalent COM ITypeLib & ITypeInfo interfaces

Information isn’t packed into structuresDeclared in COR.H / CORHDR.HCLSIDs and IIDs defined in CORGUID.LIB

Page 29: DocumentN4

The Unmanaged Metadata Interfaces

IMetaDataDispenserThe “root” interface for obtaining all othersWorks on file or memory based metadata

IMetaDataImportThe primary interface for reading class info

IMetaDataAssemblyImportIMetaDataEmitIMetaDataAssemblyEmit

Page 30: DocumentN4

DemoThe Meta program

Page 31: DocumentN4

Obtaining an IMetaDataImportInterface Instance

#include <cor.h>

CoCreateInstance( CLSID_CorMetaDataDispenser, 0,

CLSCTX_INPROC_SERVER,

IID_IMetaDataDispenser,

(LPVOID *)&pIMetaDataDispenser );

pIMetaDataDispenser->OpenScope(

szFileName, // Name of assembly file

ofRead,

IID_IMetaDataImport,// Desired interface

(LPUNKNOWN *)&pIMetaDataImport );

Page 32: DocumentN4

Important Metadata TokensmdTypeDef: A classmdMethodDef: A code member of a classmdParamDef: A parameter to a methodmdFieldDef: A data member of a classmdPropertyDef: Similar to a field, but with accessormethodsmdTypeRef: A reference to a class (possibly in another assembly)mdMemberRef: A reference to a code or data member in another modulemdModuleRef: A reference to another module (possibly in another assembly)

Page 33: DocumentN4

IMetaDataImport MethodsToken Enumeration Method Get Properties method

mdTypeDef EnumTypeDefs GetTypeDefProps

mdMethodDef EnumMethods GetMethodProps

mdFieldDef EnumFields GetFieldProps

mdEvent EnumEvents GetEventProps

mdParamDef EnumParams GetParamProps

mdToken EnumMembers GetMemberProps

mdProperty EnumProperties GetPropertyProps

mdMemberRef EnumMemberRefs GetMemberRefProps

mdModuleRef EnumModuleRefs GetModuleRefProps

mdTypeRef EnumTypeRefs GetTypeRefProps

mdPermission EnumPermissionSets GetPermissionSetProps

mdString EnumUserStrings GetUserString

mdCustomValue EnumCustomAttributes GetCustomAttributeProps

mdInterfaceImpl EnumInterfaceImpls GetInterfaceImplProps

This List Is Not Complete!

Page 34: DocumentN4

Example: Enumerating All Methods In An Assembly::EnumTypeDefs returns an array of mdTypeDefs

Each representing one class::GetTypeDefProps returns the name of the class (and other assorted info)Pass each mdTypeDef to ::EnumMethods to get an array of mdMethodDefs

One per method in the class::GetMethodProps returns the name of the method (and other assorted info)

Page 35: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 36: DocumentN4

Type Signatures

Signatures are very similar in concept to C++ name mangling

They both provide a minimal level assurance that the calling code matches up with the called code

In .NET, all methods and fields have a signature associated with them.NET signatures are a sequence of bytes that describe “type” information and other details

For example, the return value and parameter types of a method

Page 37: DocumentN4

Type Signature Encoding

Format for methods:Calling convention (BYTE)Number of parameters (BYTE)Return value type (Variable length)Parameter types (Variable length)

CorElementType enum in CORHDR.H defines “base” typesMore complex types (e.g., pointers) are built out of base types and modifiers

Page 38: DocumentN4

Type Signature Example

long FirstFunction( long j, float k);

0x00 CorCallingConvention::IMAGE_CEE_CS_CALLCONV_DEFAULT

0x02 Number of parameters

0x08 Return type: CorElementType::ELEMENT_TYPE_I4

0x08 Param 1 type: CorElementType::ELEMENT_TYPE_I4

0x0C Param 2 type: CorElementType::ELEMENT_TYPE_R4

becomes…

Page 39: DocumentN4

Drilling Into Metadata TokensThe DWORD tokens used by the unmanaged interfaces are easily decodedThe top BYTE indicates the token type

See CorTokenType in CORHDR.HThe bottom 3 BYTEs are an array index

Indices start at 0, and increase monotonically through the assemblyThus, a maximum of 24MB of any particular token type in an assembly

Page 40: DocumentN4

Token Examples

Example tokens0x02000034 = The 0x34th TypeDef in the assembly0x06000159 = The 0x159th method in the assembly

typedef enum CorTokenType

{

mdtModule = 0x00000000,

mdtTypeRef = 0x01000000,

mdtTypeDef = 0x02000000,

mdtFieldDef = 0x04000000,

mdtMethodDef = 0x06000000,

Page 41: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 42: DocumentN4

ILDASM

“Intermediate Language Disassembler”Displays IL and metadata for an assemblyDisplays metadata in a nice, graphical hierarchy

But information is not necessarily completeSemi-undocumented /ADV optionDemo

Page 43: DocumentN4

Aisto’s Reflector

A much more complete Metadata viewer than ILDasmDemoLutz Roeder’s “Reflector” shows assembly resources, and lets you save them to a fileDemo 2: Documentation.xsl from Reflectorhttp://www.aisto.com/roeder/dotnet/

Page 44: DocumentN4

MetaInfo

Written in C++ and the unmanaged APIMicrosoft’s most complete metadata toolSources in <.NET SDK>

.\tool developers guide\samples\metainfoDemo

Page 45: DocumentN4

TypeRefViewer

DemoShows imported namespaces / classes / methodsFrom the November 2001 MSDN magazine, including source

Page 46: DocumentN4

Agenda

What Is Metadata?Metadata Object OverviewThe Reflection APIThe Unmanaged APIType Signatures & TokensTools And DemoWrap-up And Resources

Page 47: DocumentN4

Wrap-up And Resources

.NET SDK.\include\cor.h, .\include\corhdr.h.\tool developers guide\samples\metainfo.\tool developers guide\docs\

• “Assembly Metadata Unmanaged API.doc”• “Metadata Unmanaged API.doc”

Reflector program: http://www.aisto.com/roeder/dotnet/Avoiding DLL Hell, Matt Pietrek, October 2000 MSDN magazine (Meta program)March 20001 MSDN magazine (MetaViewer)November 2001 MSDN magazine (TypeRefViewer)