日期:2008-09-21  浏览次数:20448 次

原文出处:http://www.codeguru.com/Csharp/Csharp/cs_syntax/attributes/article.php/c5831/

Using Attributes in C#
Rating: none

Sadaf Alvi (view profile)
September 24, 2002



Environment: C#

Introduction
Attributes are a new kind of declarative information. We can use attributes to define both design-level information (such as a help file or a URL for documentation) and run-time information (such as associating an xml field with a class field). We also can create "self-describing" components using attributes. In this tutorial, we will see how we can create and attach attributes to various program entities, and how we can retrieve attribute information in a run-time environment.

Definition

(continued)



As stated in MSDN (ms-help://MS.MSDNQTR.2002APR.1033/csspec/html/vclrfcsharpspec_17_2.htm):

"An attribute is a piece of additional declarative information that is specified for a declaration."
Using Pre-Defined Attributes
There is a small set of pre-defined attributes present in C#. Before learning how to create our own custom attributes, we will first look at how to use those in our code.

using System;
public class AnyClass
{
[Obsolete("Don't use Old method, use New method", true)]
static void Old( )
{
}
static void New( )
{
}
public static void Main( )
{
Old( );
}
}
Take a look at the this example. We use the attribute Obsolete, which marks a program entity that should not be used. The first parameter is the string, which explains why the item is obsolete and what to use instead. In fact, you can write any other text here. The second parameter tells the compiler to treat the use of the item as an error. The default value is false, which means the compiler generates a warning for this.

When we try to compile the preceding program, we will get an error:

AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'
Developing Custom Attributes
Now we will see how we can develop our own attributes. Here is a small recipe to create our own attributes.

Derive our attribute class from the System.Attribute class as stated in the C# language specification (A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration) and we are done.

using System;
public class HelpAttribute : Attribute
{
}
Believe me or not, we have just created a custom attribute. We can decorate our class with it as we did with an obsolete attribute.

[Help()] public class AnyClass
{
}
Note: It is a convention to use the word Attribute as a suffix in attribute class names. However, when we attach the attribute to a program entity, we are free not to include the Attribute suffix. The compiler first searches the attribute in System.Attribute derived classes. If no class is found, the compiler will add the word Attribute to the specified attribute name and search for it.

But this attribute does nothing useful so far. To make it a little more useful, let's add something more in it.

using System;
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description; public String Description
{
get {
return this.description;
}
}
}
[Help("this is a do-nothing class")]
public class AnyClass
{
}
In above example, we have added a property to our attribute class. We will query it at runtime in the last section.

Defini