Sonali Gogate
Attributes provide us with generic means of associating information with
different elements. These can be defined by programmers and attached to elements
in code such as classes, properties, methods and data members. Once an attribute
has been associated, it can be queried at runtime using reflection.
Attributes could be used to define design time information or run time
information or even runtime behavior.
Standard Attributes
C# provides a number of standard attributes. Following table lists commonly
used standard attributes —
Attribute | Description |
attributeusage | to determine what elements an attribute can be applied to |
conditional | conditional inclusion of methods |
guid | specifies a guid |
in | indicates an in parameter |
nonserialized | makes member or property transient |
obsolete | marks an entity obsolete (Compiler would warn on usage) |
out | marks an out parameter |
returnshresult | method return a HRESULT |
serializable | makes class or structure serializable |
A lot of these standard attributes are used to allow C# code to work with
unmanaged code.
Using Attributes
To use an attribute, it must be placed in square brackets on the line before
the element that they describe.
Here is an example of using the conditional attribute.
public class myClass {// conditional inclusion of a method public void TraceInfo() { Console.WriteLine(“Output Trace Info …”); } }
In this example the conditional
attribute takes name of a preprocessor symbol as an argument.
And how would this method get called? Here is the sample code —
#define DEBUG public class myMain { public static void main() { myClass mclass = new myClass(); ... // if DEBUG was not defined following line would be ignored. mclass.TraceInfo(); ... } }
Note that the #define DEBUG
statement needs to appear before both the definition of the conditional method
and usage of the same, for both of those to get compiled.
Defining Custom Attributes
To define your own attribute you must first create an attribute class and
this class must inherit from System.Attribute
base class.
While defining attributes, attributeusage
attribute is used to define which elements in a program they can be applied to.
We can specify AttributeTargets.All
if we don’t care what elements the attribute is to be applied to. Attribute
classes must have at least one public constructor.
Attributes can take 2 types of parameters, positional and named -
Positional parameters are used as arguments to constructors and must be
specified every time the attribute is used.
Named parameters are optional and when they are used, they can be
identified by their names. They are represented by non-static data member or
property in the attribute class.
The .NET types that can be used for attribute parameters are —
bool, byte, char, double, float, int, long, short, string, object,
System.Type
We could also use a public enum or
arrays of the above types.
Here is an example of defining and using custom attribute —
using system; // this attribute can be applied only to classespublic class AuthorInfo : System.Attribute { private string name; private string date; public AuthorInfo(string name) { this.name = name; } public string Name { get { return name; } } public string Date { get { return date; } set { date = value; } } }
In this example, name is a positional parameter while date is a named
parameter. We have added only a get property for name since it is set via the
constructor.
As in case of standard attributes, to use these custom attributes, we simply
have to include it at the top of the element it applied to (in case of the
attribute in above example only a class.)
Here is how you would use the attribute we just defined —
public class myTestClass { ... }
Note that Date="20/08/00"
can be omitted.