Module.ResolveType(int, Type[], Type[]) example

Module.ResolveType(int, Type[], Type[]) example

Post by Sacha Faus » Tue, 19 Jul 2005 17:31:12

Anyone as a code sample that use the new Module.Resolve(int, Type[], Type[])
call in framework 2.0?

Module.ResolveType(int, Type[], Type[]) example

Post by Gael Frait » Wed, 20 Jul 2005 18:28:23

PostSharp uses it. You can download the code at
little difficult to understand the whole flow...

Maybe the following will help:

These methods (ResolveType, ResolveMethod, ...) should be used in the
context of a disassembler or "binary IL reader".

In binary IL code, methods, fields, types and so on are referenced to by
"tokens", i.e. integers. These integers refer to entries in the metadata

A typical use scenario involving ResolveType is:

1. You want to disassemble a method.
2. You get the MethodBase of the method to disassemble. Do the following to
get the generic arguments in the context of this method.

Type[] typeGenericArguments = method.DeclaringType.GetGenericArguments() ;
Type[] methodGenericArguments = method.GetGenericArguments();

3. You get a byte array containing the IL code.

4. While reading this byte array, you discover an instruction expecting a
type metadata token (for instance a cast instruction).

5. You want to know what Type refers the token to. Use:

Type type = module.ResolveType( token, typeGenericArguments,
methodGenericArguments );

Hope it helped.