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.

Comments (4)

  1. 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.

  2. José Lema says:

    Framework Design Guidelines:Conventions, Idioms, and Patterns for Reusable .NET LibrariesAuthors: Brad

  3. OrlandoCurioso says:

    Error:

    ‘where T : struct’ becomes ‘where T : ValueType, struct’