27.1 First look at attributes


Let's look at a first example of attributes. In the code fragment below, each method is tagged with an Author attribute: [5]

[5] Another way to put it is that the three methods are the targets of the [Author] attribute.

 1:  // MyClass.cs  2:  using System;  3:  4:  public class MyClass{  5:  6:  [Author("Mok","21 Dec 02")]   <-- 1   <-- 2  7:    public void DoSomething(){  8:      // some code  9:    } 10: 11:  [Author("Mindy","22 Dec 02")]   <-- 2  12:    public void DoSomethingElse(){ 13:      // some code 14:    } 15: 16:  [Author("Abigail","27 Nov 02")]   <-- 2  17:    public void DoNothing(){ 18:      // some code 19:    } 20:  } 

(1) Parameters passed into the attribure instance

(2) Attribute specification

I use the term 'attribute specification' to describe the attribute tags used in the source code.

In this example, it may be that several developers are doing concurrent development work on the same class. They have been instructed by the team leader to update the Author attribute to reflect the name of the developer who last made a change to each method, and the date the change was made. In this case, the Author attribute takes in two string parameters “ a name and a date (both as strings).

During compilation, these attribute values are actually stored as metadata in the resultant .NET assembly file. These values can be obtained during runtime by another .NET application via reflection. In this case, the other application can dynamically load the MyClass assembly into memory and use C#'s reflection API to extract the attributes and their values for each method “ and display them in a text file. For the example here, this text file can be used as some kind of interim documentation for the source code.

We shall revisit the Author attribute again.

After looking at the coded example, I hope you have got the idea that attributes are tags in the source codes which provide additional information, and which is stored in the assembly together with the IL codes (attribute information is not stored in the IL codes, but in the assembly file together with the IL codes they describe). This information can be used during runtime via reflection in any creative manner. Not only can attributes be used to describe methods, they can also be applied to classes, interfaces, assemblies, method parameters, and other entities. Here is an example of how a class is tagged with a class attribute called Version .

 1:  [Version("1.1")]  2:  public class ClassA{ 3:    // codes 4:  } 5: 6:  [Version("1.0")]  7:  public class ClassB{ 8:    // codes 9:  } 

Having seen attributes at work, let's carry on with our introductory discussion.

There are two types of attributes “ see Figure 27.1:

  • Custom attributes. These are attributes written and used by yourself. They will be covered in Chapter 28.

  • Standard attributes. These are attributes built into the .NET framework. Standard attributes [6] are predefined, and recognized by the C# compiler or CLR.

    [6] Standard attributes have also been called 'intrinsic attributes' or 'predefined attributes' in some books.

Figure 27.1. C# attributes can be classified into custom or standard attributes. Standard attributes can be further subdivided into two subgroups.

graphics/27fig01.gif

Standard attributes can be further subdivided into two groups:

  • Attributes reserved by the C# language itself. There are only three of these “ [Obsolete] , [AttributeUsage] , and [Conditional] . These attributes are recognized by the C# compiler, and documented in the C# language specification.

  • Attributes built into the .NET framework. These are attributes coded by Microsoft engineers when writing the .NET BCLs. Examples include the [WebMethod] and [DllImport] attributes.

Use of an attribute is not all magic “ you require a corresponding attribute class to be written. In the case of [Author] and [Version] , as you have seen, there have to be corresponding attribute classes called AuthorAttribute and VersionAttribute coded somewhere, and which the compiler must be able to find during compilation.

The remainder of this chapter will discuss some important standard attributes and show how to use them. Writing custom attributes is a difficult topic in itself “ it has the entire next chapter dedicated to it.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net