Sometimes, I use attributes to decorate classes or methods and use reflection to get the 'attributed' data.
Maybe a bit difficult to explain, but the last thing for which I've used attributes, is in a system where I have a couple of entities in a database.
Each entity has some kind of 'code', and each entity can also have some interpretation rules.
In my project, I have one entity class, which represents an entity that exists in the Database, and, I also have a set of 'Rule' classes.
One Rule class contains the interpretation logic of a given entity.
In order to 'link' a certain 'Rule' (interpretation) to a specific instance of my entity, I've created a custom Attribute.
I decorate my 'Rule' class with this attribute, and through the attribute, I define for which entity this is a Rule.
Then, when I load an entity from the DB, I inject the correct rule into that entity.
A little bit of code to make things clear:
public class MyEntity
{
public string Code
{
get;
private set;
}
public bool IsValidFor( ... )
{
IRule rule = RuleRegistry.GetRuleFor(this);
if( rule.IsValid() ) ...
}
}
[RuleAttrib("100")]
public class MyRule : IRule
{
public bool IsValid()
{
}
}
This is just a little example, but I think you'll catch the drift.
The RuleAttrib attribute on the MyRule class, says that this is a Rule that should be applied to the instance of MyClass which has a code "100".
The RuleRegistry instance is able to retrieve the correct IRule for the current Entity (using reflection).
Another example in where I've used attributes, in combination with Postsharp, is the implementation of a 'locking' system:
http://fgheysels.blogspot.com/2008/08/locking-system-with-aspect-oriented.html