1. Home
  2. Computing & Technology
  3. Delphi Programming

An Introduction to the .NET Framework Class Library
Attributes and the System.Attribute Type

By Zarko Gajic, About.com

Attributes are used to decorate types and type members, such as fields, properties and methods. Attributes are saved with the metadata inside a .NET Framework application or assembly. At runtime, one can query types or instances to see if they have certain attributes, or properties on those attributes.

General attribute syntax for both Delphi and C# is basically the same, and immediately precedes the symbol that it modifies:

~~~~~~~~~~~~~~~~~~~~~~~~~
[AttributeTypeName(Parameter, Parameter)]
public class Foo { ... }
~~~~~~~~~~~~~~~~~~~~~~~~~

One thing to note: Attributes, by convention, generally always end in the word Attribute. For example, DesignerAttribute and ComVisibleAttribute. Most compilers allow you to leave off the "Attribute" portion of the type name.

What is a good use of an attribute? Most development environments have a special design time class that allows you to manipulate a component or control at design time. The DesignerAttribute allows one to easily do this:

~~~~~~~~~~~~~~~~~~~~~~~~~
[DesignerAttribute("MyControlDesigner")]
public class MyControl : System.Windows.Forms.Control
~~~~~~~~~~~~~~~~~~~~~~~~~

When a WinForms designer attempts to create a designer for MyControl, it will find the DesignerAttribute on the class and create an instance of the MyControlDesigner for it.

Assembly level attributes are useful for other things, such as specifying the version, author, company name, etc. for the assembly:

~~~~~~~~~~~~~~~~~~~~~~~~~
[assembly: AssemblyTitle("This is my cool assembly")]
[assembly: AssemblyDescription("It does neat stuff")]
[assembly: AssemblyCompany("Borland Software Corporation")]
[assembly: AssemblyVersion("1.0.3.5")]
~~~~~~~~~~~~~~~~~~~~~~~~~

Let's see how to get the attribute we are after from a class...

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Learn Delphi for .NET
  5. An Introduction to the .NET Framework Class Library

©2009 About.com, a part of The New York Times Company.

All rights reserved.