Team LiB
Previous Section Next Section

The "Other" Kind of Attribute

Throughout most of this book we've been using the term "attribute" in the generic OO sense to represent a state-describing data element of a class, also known in C# as a "field." We mentioned back in Chapter 3 that there is also a .NET-specific programming element called an attribute. Although we don't make use of this .NET attribute in building the SRS, we'd like to at least provide a brief description of this "other" kind of attribute.

A (.NET) attribute is a reference type that derives from the System.Attribute class, and is used to assign metadata tags to other programming elements—that is, descriptive information about such elements.

An attribute can be applied to any code element (class, constructor, delegate, method, field, etc.) by placing the name of the attribute in brackets before the code element. For example, the System.Array class has an associated attribute named SerializableAttribute as shown here:

[SerializableAttribute]
public abstract class Array : ICloneable, IList, ICollection, IEnumerable { ... }

The application of this attribute to the Array class indicates that Array objects are serializable, meaning that they can be stored to or restored from disk in binary form.

As programmers, we could certainly determine that Arrays are serializable from inspecting the C# language documentation; so, what does an attribute buy us? The "knowledge" that is imparted through the application of an attribute can be programmatically "discovered" at run time, through a mechanism known as reflection. (Going into the full details of how reflection works is beyond the scope of a beginning-level book such as this to address.)

We can apply any of the predefined attributes provided by the FCL to our own code. For example, we can place the predefined Obsolete attribute before the header of a method that we wish to flag as obsolete, to indicate that this method is perhaps going to be phased out at some later date so as to discourage its use:


public class MyClass
{
  // We're adding an attribute to Foo().
  [Obsolete]
  public void Foo() {
    // Method details omitted ...
  }

  // Other details omitted.
}

If client code subsequently attempts to invoke the Foo method on an instance of MyClass:

public class AttrDemo {
  static void Main() {
    MyClass x = new MyClass();

    // We're trying to call a method that has been
    // tagged as obsolete.
    x.Foo();
  }
}

then when the AttrDemo class is compiled, the compiler will detect that a method that has been tagged as obsolete is being called on an object, and will generate the following warning:

warning CS0612:   'MyClass.Foo()' is obsolete

The program can still be executed, but the programmer has been "warned" that using the Foo method may not be advisable.

Note?/td>

We can also create user-defined attributes for whatever specific metadata needs we might have, but this topic is beyond the scope of this book to address.


Team LiB
Previous Section Next Section