RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Take Control of .NET Using Custom Attributes

Don't content yourself with the attributes that are built in to .NET. Write custom attributes to add your own meta-data to properties, methods, classes, and other program elements, giving you fine-grain control.

ET has more than 100 attributes that are used to control XML serialization, Web services, COM interoperability, and more. For example, XmlElementAttribute is used to control the element name and data type used when serializing a class to XML, and AssemblyVersionAttribute is used to assign a version number to an assembly.

But you aren't limited to using the ones that Microsoft built. Custom attributes allow the developer to store meta-data against program elements, and that data can be accessed at runtime to control program actions. This article will show you how to create your own custom attributes in .NET and will give you a practical example of how to use them.

Creating Your Own Attributes
To create your own custom attribute class, simply create a new class that inherits System.Attribute. Add whatever properties you want (normally you will also add these to your attribute class constructor), and your custom attribute class is ready for use.

The next step is to determine what elements your attribute can be used on. By default, custom attributes can be applied to all program elements. But you can restrict which types of program elements your custom attribute can be applied to by using the AttributeUsage attribute. The constructor for AttributeUsage takes an enumeration argument of type AttributeTargets (this enumeration is a bit mask, so you can combine values with a bitwise or operation).

You can use the other arguments to AttributeUsage to control whether multiple instances of the same attribute are allowed, and whether derived classes inherit your custom attribute. For example, you could use these arguments to define a custom attribute that can be used with a property only, is inherited by derived classes, and can be used only once for each property use. The code to implement those conditions is shown:

< _
    System.AttributeUsage( _
      System.AttributeTargets.Property, _
      AllowMultiple:=False, _
      Inherited:=True) _
  > _
  Public Class ListDisplayAttribute
    Inherits System.Attribute
    ' Code goes here
  End Class
Using Attributes in Code
Attributes can be applied to many elements in .NET code, including assemblies, classes, properties, events, and enumerations. This code demonstrates the same attribute, MonitoringDescription, applied to a class and to a property.
<Diagnostics.MonitoringDescription("My Demo Class")> _
Public Class TestClass
  <Diagnostics.MonitoringDescriptionAttribute("My Test Property")> _
  Public ReadOnly Property TestProp() As String
    End Get
  End Property
End Class
By convention, classes that inherit from System. Attribute have an "Attribute" suffix added to their names, as in Diagnostics.MonitoringDescriptionAttribute, above. You can use the class name without the "Attribute" suffix when you use attributes in code. In the example above, I used MonitoringDescription on the class and MonitoringDescriptionAttribute on the property. Both are interpreted the same way. This can be a bit confusing because if you are looking for help in the .NET framework documentation, you have to use the "real" class name—that is, use MonitoringDescriptionAttribute when you search or you won't find the help topic you are looking for.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date