Home > Articles > Programming > Windows Programming

Creating Custom Attributes in Visual Basic .NET

  • Print
  • + Share This
.NET relies on metadata that is deployed with your assembly to solve problems like "DLL Hell." Metadata is associated with .NET assemblies—that is, applications—with attributes. Attributes are simply classes used to adorn code with additional information. The kinds of additional information includes important things like security, Web Services, debugger instructions, component/IDE instructions, and much more. In this article you will learn how to create a custom attribute that might be used with an application builder.
Meet Paul Kimmel at the November 2002 Fall Comdex in Las Vegas at the "Great Debate: .NET or .WHAT?" and get him to sign your copy of Advanced C# Programming (Osborne/McGraw-Hill, 2002) or Visual Basic .NET Unleashed (Sams, 2002).
Like this article? We recommend

Like this article? We recommend

Creating the Attribute Project

You can define a class in any project and any source file. Technically, attributes are just classes. However, because you use attributes to decorate elements of code, you may want to use them in more than just one particular project. For this reason, it's a good idea to create attributes in a separate class library assembly.

You can use the Class Library project template to create a new class library project. The Class Library project template uses the Wizard Engine to make a copy of a .vb file and project containing an empty class. You can use this as a starting point for defining a custom attribute that conveys information about the kind of control you may want to associate with a property. The general idea is that you might create an attribute like this if you were inclined to create an application generator. Be sure to provide a meaningful name for the project; I named the class in Listing 1 ControlDesignerAttribute.

Listing 1—The ControlDesignerAttribute Class Is Our Beginning Point

Public Class ControlDesignerAttribute
End Class


By convention, attributes have an Attribute suffix when the attribute is defined. Also by convention, the Attribute suffix is dropped when you apply the attribute.

  • + Share This
  • 🔖 Save To Your Account