Welcome to MSDN Blogs Sign in | Join | Help

Kathy Kam

Reflection on the CLR and .NET
TxRef update

If you opened up your CD from Krzysztof Cwalina and Brad Abram's new book Framework Design Guidelines. You will notice a nifty tool named TxRef. Unfortunately, the tool was written against "Whibey Beta", and some code needs to be updated for "Whidbey RTM".

Here is the updated code:

void WriteTypeConstraints(Type type)
    {
        foreach (Type t in type.GetGenericArguments())
        {
            bool writeComma = false;
            Type[] arr = t.GetGenericParameterConstraints();
            GenericParameterAttributes ga = t.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;

            if ((arr.Length > 0) || (ga != GenericParameterAttributes.None))
            {
                Write(" where ");
                WriteTypeName(t);
                Write(":");
            }
            for (int i = 0; i < arr.Length; i++)
            {
                WriteTypeName(arr[i]);
                if (i < arr.Length - 1) Write(",");
            }

            if (arr.Length > 0)  writeComma = true;
            if ((ga & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
            {
                if (writeComma) Write(", ");
                Write("class");
                writeComma = true;
            }

            if ((ga & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0)
            {
                if (writeComma) Write(", ");
                Write("struct");
                writeComma = true;
            }
            else if ((ga & GenericParameterAttributes.DefaultConstructorConstraint) != 0)
            {
                if (writeComma) Write(", ");
                Write("new()");
            }
        }
    }

Brad also talked about this particular breaking change in his blog here. I want to personally thank Andy for finding and reporting this error.

Posted: Wednesday, November 16, 2005 3:49 PM by KathyKam
Filed under:

Comments

Ayende Rahien said:

Do you have any idea how to do the same for a method?
I couldn't find a way to get the generic method arguments with their constraints.
# November 17, 2005 12:55 AM

José Lema said:

Framework Design Guidelines:Conventions, Idioms, and Patterns for Reusable .NET LibrariesAuthors: Brad
# June 4, 2006 5:26 AM

OrlandoCurioso said:

Error:

'where T : struct' becomes 'where T : ValueType, struct'

# September 24, 2007 2:50 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker