Generics and the special constraints

Someone recently asked me to update an old post of mine Reflection and Generics . You gotta be careful what you write in blogs as they seem to live forever.

Well, it is true, this needs a little updating. We no longer use the NewConstraintAttribute (or the other special constraint attributes) to denote the special constraint in the metadata. (In fact they are being removed from Whidbey)

Here are some examples in C# syntax:

class C<T> where T : new() { } // T must have a public default constructor

class C<T> where T : class { } // T must be a class

class C<T> where T : struct { } // T must be a struct

Here are some examples in VB syntax:

Class C(Of T As New)

End Class

Class C1(Of T As Class)

End Class

Class C2(Of T As Structure)

End Class

We now encode it the IL syntax for special constraints by putting either '.ctor', 'class' or 'valuetype' before the type parameter name (C<T> above is minimally 'C`1'<.ctor T> in IL).

So, some code to show how this works in the IL:

class Class1

{

    static void Main()

    {

        GenericParameterAttributes ga = typeof(C<>).GetGenericArguments()[0].GenericParameterAttributes;

        if ((ga & GenericParameterAttributes.SpecialConstraintMask) == GenericParameterAttributes.DefaultConstructorConstraint)

        {

            Console.WriteLine("Has New Constriant");

        }

        else

        {

            Console.WriteLine("Does not have New Constriant");

        }

    }

}