October 06, 2003 10:54 am

Instantiating generic types

The first thing which needs to be done is getting Type.GetMethods() right wrt. generic instances.

Suppose you have a generic type definition like this:

public class Stack<S> { public void Hello (S s) { } } public class Test<T> : Stack<T> { public void Foo (T t) { } }

Now, you want to instantiate Test to Test<int> - and you want to do this in the same assembly. This means that the underlying generic type is actually a TypeBuilder.

We need to consider two things here:

  • We can't just use klass->methods, but need to access the tb->methods array on the TypeBuilder instead. We still need to inflate the methods and we should just do this once.
  • When calling such a method, we need to insert a metadata fixup token and do a fixup just like we do it for a normal MethodBuilder.

I'm using a small trick to do this: the actual instantiation of a generic type is done in mono_reflection_bind_generic_parameters() which is called from Type.BindGenericParameters(). If the underlying generic type is a TypeBuilder, we populate iklass->methods from tb->methods and tb->ctors.

Posted by martin at October 06, 2003 10:54 am.