Defining Attributes


Microsoft has created a number of attributes to apply to various programming elements. You can also define your own attributes. Of course yours won't have any effect unless you write code that specifically looks for the attributes and does something with the information. Remember that attributes are there to extend the information about an element.

To define a custom attribute:

  1. Write a class where the class name has the suffix attribute, for example: public class VisibleColumnAttribute .

  2. Derive the class from System.Attribute .

  3. Add one or more constructors to the class. If you add only parameterized constructors then the parameters of the constructor will become required arguments for the attribute. For example: VisibleWidthAttribute(bool value) { }

  4. Add public fields or properties to the attribute. Public fields or properties define optional arguments for the attribute. For example: public bool ReadOnly ;

  5. Apply the attribute to any programming element using square brackets. For example: [VisibleColumn(true,ReadOnly=true)] ( Figure 12.28 ).

    Figure 12.28 A custom attribute is just a class that's derived from System.Attribute. The parameters of the constructor will be the parameters of the attribute.
     public class VisibleColumnAttribute : System.Attribute {    private bool _visible;    public bool IsReadOnly;    public VisibleColumnAttribute(    bool value)    {       _visible=value;    } } 

graphics/tick.gif Tips

  • An attribute is a class that derives from System.Attribute .

  • By default the attribute can be applied to any programming element: assembly, class, members , delegates, events, parameters, etc. You can control the usage for the attribute by applying an attribute to the attribute. Let me explain. The attribute you apply to the attribute is called System.AttributeUsage . This attribute has one parameter, a constant from the enum System.AttributeTargets . The constants in this enum include: All, Assembly, Class, Constructor, Delegate, Enum, Event, Field, Interface, Method, Module, Parameter, Property, ReturnValue , and Struct . You can combine the constants in this enum using the operator. For example: [AttributeUsage(AttributeTargets.Class AttributeTargets.Delegate)] tells the compiler that the attribute can only be applied to classes and delegates ( Figure 12.29 ).

    Figure 12.29 AttributeUsage is an attribute for your attribute class. It controls how the attribute can be applied.
      [AttributeUsage(AttributeTargets.   Property)]  public class VisibleColumnAttribute : System.Attribute {    private bool _visible;    public bool IsReadOnly;    public VisibleColumnAttribute(    bool value)    {       _visible=value;    } } 
  • If you add a default constructor, then the attribute can be applied without arguments. If you add only parameterized constructors, then the compiler requires that you add arguments when applying the attribute. After specifying the arguments for a constructor you can set any public fields or properties for the attribute by typing the name of the field followed by the equal sign and the value of the field ( Figure 12.30 ).

    Figure 12.30 There are two types of parameters for attributes: named and unnamed. Named parameters correspond to a parameter in the attribute's constructor. Unnamed parameters correspond to public fields or properties in the class.
     [VisibleColumn(true,  IsReadOnly=true  )] public string Name {    get    {       return _name;    }    set    {       _name = value;    } } 
  • By default, an attribute can be applied only once to a programming element. For example if the attribute is ColumnWidth , you can't say [ColumnWidth(500),ColumnWidth(300)] , because that's illegal. However, at times it may make sense to apply the same attribute twice. Imagine an attribute named Author that lets you specify the developers that worked on a class. In that case you may enter [Author("Jose"), Author("Bill")] . To allow an attribute to be applied twice, you can set the public field AllowMultiple in AttributeUsage to true ( Figure 12.31 ).

    Figure 12.31 By default, the same attribute can't be applied more than once to the same element. With the AllowMultiple setting in AttributeUsage you can change that behavior.
     [AttributeUsage(AttributeTargets.All,  AllowMultiple=true  )] class Author : System.Attribute {    public string Name;    public Author(string val)    {       Name = val;    } } 
  • By default, attributes are inheritable. That means that if you apply an attribute to a base class and then write a derived class, the derived class gets all the attributes from the base class. In the same fashion if the attribute is applied to a virtual method and you override the method in the derived class the attribute is available in the derived class. You can prevent this behavior by setting the Inherited field of the AttributeUsage class ( Figure 12.32 ).

    Figure 12.32 By default, attributes are inherited. That means that derived classes get the attributes of the base class. For the Author attribute that may not make sense since the author of the base class isn't necessarily the author of the derived class.
     [AttributeUsage(AttributeTargets.All,  Inherited=false  )] class Author : System.Attribute {    public string name; } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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