2007-06-03

c# - Custom Attributes

From time to time, you may find it particularly useful to create custom attributes for fields, properties, classes, methods, etc.

Creating Custom Attributes is relatively easy, and if you're good with Reflection, they can save you a lot of hassle. More on reflection coming soon, but here's your custom attribute:
public class CustomAttribute : System.Attribute
{
   // n number of Values Associated with your Attribute
   private int _customValue;
   public int CustomValue
  {
    get { return _customValue; }
  }

   // Constructor - can be overloaded, if necessary
  public CustomAttribute (int CustomValue)
  {
    _customValue = CustomValue;
  }
}
And how to implement it...
[CustomAttribute(5)]
public int CustomProperty
{
  get { return _customProperty; }
  set { _customProperty = value; }
}

No comments: