Custom Attributes
If you would like to adorn your classes with some custom meta-data, Custom Attributes are the way to go.
First, start with your class.
public class MyDumbClass
{
public MyDumbClass()
{
// do nothing
}
}
Now we want to say some things about this class... like, is it a good class?
We create an attribute where we can specify this info.
Create a class that inherits from 'Attribute'.
Add some fields to the class.
public class MySpecialAttribute : Attribute
{
public string GoodAdvice { get; set; }
public bool ThisClassIsGood { get; set; }
}
Now you can find in your intellesense the "MySpecial" attribute.
[MySpecial(ThisClassIsGood = true, GoodAdvice="Do no harm")]
public class MyDumbClass
{
public MyDumbClass()
{
// do nothing
}
}
The attribute name, as you can see, is the name of the attribute class without the 'Attribute' text.
Because there can never be to much good in the world, you may wish to add the attribute several times.
Doing so will give you an error unless you specify the attribute can be used multiple times.
On the Attribute class, add an AttributeUsage attribute:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]
public class MySpecialAttribute : Attribute
{
public string GoodAdvice { get; set; }
public bool ThisClassIsGood { get; set; }
}
We chose 'AttributeTarget.Class' because we are using it on a class. If you want the attribute used on a field, or method or something else, you can specify AttributeTargets.Method or whatever you need.
Custom Attributes
I was asked about Custom Attributes and thought it would make a good blog entry.
Custom Attributes are added to properties of a class. They give extra meaning to these properties.
Here is an example of what they look like and how they are used:
class MyClass
{
[SearchablePropertyAttribute(DisplayType = "Primary")]
public string FirtName
{
get { return firtName; }
set { firtName= value; }
}
[AddressPropertyAttribute(Order = 1, ValidationRequired = true)]
public string Address
{
get { return address; }
set { address= value; }
}
}
SearchablePropertyAttribute and AddressPropertyAttribute are both classes I created. They must extend System.Attribute.
You assign the values to these properties in the class as shown above.
They can be as simple as this:
public class SearchablePropertyAttribute : Attribute
{
public SearchablePropertyAttribute() { }
public string DisplayType = string.Empty;
}
public class AddressPropertyAttribute : Attribute
{
public AddressPropertyAttribute() { }
public int Order = 0;
public bool ValidationRequired = false;
}
To use these attributes, you have to get the PropertyInfo of the object
...
MyClass myclass = new MyClass();
// Get a list of all the properties in the class
PropertyInfo[] piList = typeof(myclass).GetProperties();
// Loop thru each property and check if it is the
foreach (PropertyIfno pi in piList)
{
// Get an array of the AddressPropertyAttribute attributes
AddressPropertyAttribute[] attributes = (AddressPropertyAttribute[])pi.GetCustomAttributes(typeof(AddressPropertyAttribute), true);
// Go thru the attributes and use them as you wish
}
...