This documentation is archived and is not being maintained.

Using Custom Code Attributes for TDL Identification in Enterprise Templates

Visual Studio .NET 2003

Steven Powell
Visual Studio Team
Microsoft Corporation

February 2002

Summary: Describes how to associate an attribute with a class and use the attribute-value pair to uniquely identify any class in your code so that your policy can control the use of the class. Readers should be familiar with Visual Studio .NET and Enterprise Templates. (4 printed pages)


Visual Studio® .NET Enterprise Templates provides some methods of uniquely identifying classes and interfaces. Without making modifications to your sources, you can identify classes using base class or the interfaces implemented by that class. However, this method does not work when you need to uniquely identify a class that does not derive from a base class or that cannot be uniquely identified by base class; for example, when you must identify several classes that share a common base class. For this situation, you can use another method of identification: code attributes.

Defining a Custom Attribute

You can use either Visual C#™ or Visual Basic® to associate a custom attribute with a class. You can then use that attribute and its value to uniquely identify any class in your application or framework. Define the custom attribute class as follows:

[Visual C#]

    [AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]
    public class PolicyClassType : Attribute
        public PolicyClassType(string className)
            this.className = className;
        public string className
                className = value;

[Visual Basic]

    ' This attribute is intended for use with Classes
    <AttributeUsage(AttributeTargets.Class)> _
        Public Class PolicyClassType
        Inherits System.Attribute

        ' Private fields
        Private m_typeName As String

        ' This constructor defines the required parameter
        Public Sub New(ByVal typeName As String)
            Me.m_typeName = typeName
        End Sub

        'Define Name property (reaonly attribute)
        Public Overridable ReadOnly Property TypeName() As String
                Return m_typeName
            End Get
        End Property
    End Class

If you define this attribute class within a particular namespace, you must give the fully-scoped name when referring to it in your policy file. In this example, it is defined within the myNamespace.OrderRequest namespace.

Associating a Custom Attribute with a Class

To assign the custom attribute to a class, do the following:

[Visual C#]

    /// <summary>
    /// Summary description for orderRequestTypeOne.
    /// </summary>
    public class orderRequestTypeOne
        // class contents deleted for clarity

[Visual Basic]

    Public Class orderRequestTypeOne
        'Class contents deleted for clarity
    End Class

The bold attribute is all that is needed to associate a custom attribute called "PolicyClassType" whose value is "TypeOne" with this class, orderRequestTypeOne.

Updating the Policy File to Identify a Class by Custom Attribute

In your policy file, the element definition identifies this class as follows:


Note that <VALUE> must include quotation marks and that the attribute is fully qualified with the namespace.


After you define this element, you can quite easily INCLUDE or EXCLUDE classes that match this element definition from a parent project or code element. This is particularly useful when you need to uniquely identify a class without a base class or must distinguish between two classes that both derive from the same base class or implement the same interface.

For another example, see Enterprise Templates: Building an Application Construction Kit. For more information on Enterprise Templates, see Enterprise Templates for Distributed Applications in the Visual Studio .NET documentation.