Last time I blogged about the formal type parameters of generic types in reflection (http://blogs.msdn.com/weitao/archive/2008/03/19/formal-type-parameters-of-generic-types.aspx). Here I will present an example in Reflection.Emit.

Reflection doesn't distinguish between a generic type/method definition and a generic type/method instantiated on its formal type parameters. So a type that represents the former can also mean the latter. In most cases this is not a problem. But in Reflection.Emit, sometimes we need to know which one it is. When a user calls ILGenerator.Emit(Opcode, Type/MethodInfo) and the Type/MethodInfo passed in is a generic type/method definition we have to know which one it is to emit the correct code because they are represented by completely different tokens in the metadata. Fortunately depending on the context, it can only mean one at any time:

  1. If we are emitting the code to call a method, this can only be a generic method instantiation, never a generic definition.
  2. If we are emitting a ldtoken instructions, we should get a method definition, not an instantiation.

I will show some sample code in the next post.