FREE BOOK

Chapter I: Attribute Fundamentals

Posted by Apress Free Book | C# Language December 09, 2008
IN THE COURSE OF DEVELOPING an application, it is quite typical to have core functionality contained in methods that are invoked by other specialized methods.

Attributes in .NET

Using attributes in your .NET code is fairly straightforward, but knowing the rules will not only help you to understand why some compilation errors may occur when you code with attributes, but it will also guide you when you create your own attributes (a topic we'll cover in Chapter 4). In this section, you'll learn how you declare attributes in your C# code, where and when attributes can be declared, and where the attribute information ends up.

Declaration Fundamentals

To demonstrate how attributes are used in C# code, we'll use the ObsoleteAttribute class to mark our BadCountry class as a class that clients should no longer use, but it will not cause an error to use BadCountry. Listing 1-5 shows the code necessary to do this.

Listing 1-5.Making a Class Obsolete via Attributes

[Obsolete]
public class BadCountry
{
    // ...
}

Attributes are declared within the brackets. Only objects whose class inherits from System.Attribute can be used within the brackets. For example, the following code would cause an error:

[DateTime(2003, 2, 19)]
public class BadCountry
{
    // ...
}

Attributes are usually named with the string "Attribute" at the end. You are not required to type in the full name when you add the attribute to your code,7 but you can if you prefer, as shown here:

[ObsoleteAttribute]
public class BadCountry
{
    // ...
}

When you add an attribute in your code, you are technically creating a new object whose information will be stored in the assembly. If the attribute has a no-argument constructor, you don't need to use parentheses in your declaration. However, as is the case with ObsoleteAttribute, attributes can define custom constructors so you can set the state of the attribute with more detail. The following code snippet uses a custom constructor of ObsoleteAttribute to set a string and boolean value.

[Obsolete("This class should no longer be used - switch to ImprovedCountry.",
false)]
public class BadCountry
{
    // ...
}

In this case, the string argument (called message) is used to define a descriptive message that clients can use to find other alternative implementations. The boolean argument (called error) is used during the client's compilation process. A true value will cause a compilation error if the deprecated item is still used; a false value will cause a compilation warning. Figure 1-3 shows the compilation output in Visual Studio .NET when BadCountry is used with error set to false.



Figure 1-3. Compilation warning messages when using ObsoleteAttribute

However, as Figure 1-4 demonstrates, when error is set to true, the client code won't even compile.



Figure 1-4. Compilation error messages when using ObsoleteAttribute

Not all attributes will affect the compilation process as ObsoleteAttribute does. In fact, attributes in themselves are harmless, unless some other process, like a compiler or the Common Language Runtime (CLR), acts on them. Although attributes will end up in your assembly, they cannot affect your code.8

NOTE Other factors are involved in using an attribute's information and acting accordingly. Chapters 2 and 3 will cover different attributes and when they come into play.

Total Pages : 7 34567

comments