August 17, 2004 5:53 pm

Generic observations

Today, I tracked a really bad boy down which caused very weird failures of gmcs compiled assemblies on the MS runtime. So, while looking at the following example, I made some interesting obervations:

interface IFoo<t> { void Test<u> (T t, U u); } class Foo<t> : IFoo<t> { void IFoo<t>.Test<u> (T t, U u) { } } class Bar<t> : IFoo<int> { void IFoo<int>.Test<u> (int a, U u) { } }
  • The most important thing is that the entries in the GenericParam table must be sorted according to the owner field.

  • In IL, we generate
    .class interface private abstract auto ansi 'IFoo`1'<T>
    

    and

    .class private auto ansi beforefieldinit 'Foo`1'<T>
           extends [mscorlib]System.Object
           implements class 'IFoo`1'<!T>
    
  • However, for the explicit interface implementation, we generate
    .method private hidebysig newslot virtual final instance void  'IFoo<T>.Test'<u>(!T t, !!U u) cil managed
    

    Note that this does not include the generic arity like in the class name.

  • To make things really confusing, we do use the arity in the .override:
    .override  method instance void class 'IFoo`1'<!T>::Test<[1]>(!0,!!U)
    
Posted by martin at August 17, 2004 5:53 pm.