views:

128

answers:

3

So a little confession, I've never written an attribute class. I understand they serve the purpose of decorating classes with flags or extra functionality possibly.

Can someone give me a quick example of not just creating and applying an attribute to a class, but rather utilizing the attribute from another block of code. The only code samples I've ever seen to utilize any form of attributes was doing so with reflection, though I've always hoped there's a way of using them without reflection.

+4  A: 

Attributes are always used with reflection. They are baked into the metadata of the types during compile time and the only way to read them is through reflection. Attributes are used when you want write a type and you want to associate some metadata with it which could be used by consumers of this type.

Darin Dimitrov
Good to know, thanks for this, given that, are they really just behaving as flags then, incapable of imparting any functionality to the class itself? I always have wondered if you could actually make an attribute that basically adds methods to a class which can only be accessed through the attribute itself..
Jimmy Hoffa
Attributes are absolutely incapable of modifying or adding any functionality to the type itself.
Darin Dimitrov
@Jimmy. Yes, attributes are basically fancy flags on classes. This property is obsolete. This class is serializable. This method is to be hidden from IntelliSense. And so on. Attributes are not for adding functionality.
Eric Lippert
Actually there are attributes that control compiler behavior. For example [Conditional] attribute may completely hide a method.
sukru
+2  A: 

First create your attribute

public class ImportableAttribute : Attribute
{

}

Then a class with a item that uses the Attribute

[ImportableAttribute]
public class ImportClass
{
    [ImportableAttribute]
    public string Item {get; set;}
}

Then check if that property uses that attribute. Can be done with classes to.. Of course :)

PropertyInfo property = typeof(ImportClass).GetProperty("Item");

if (property.IsDefined(typeof(ImportableAttribute),true))
{
     // do something
}

With a class:

typeof(ImportClass).IsDefined(typeof(ImportableAttribute), true);
Andreas
Thanks, this looks like a very simple way of doing it and answers my question, though after what Eric said I doubt as though I would ever actually use it..
Jimmy Hoffa
I use it all the time, mostly with validation (with use of xVal) but also other things.
Andreas
+4  A: 

The simplest and most elegant way to use an attribute from another block of code is to use a property instead of an attribute.

See http://blogs.msdn.com/b/ericlippert/archive/2009/02/02/properties-vs-attributes.aspx for a discussion of the differences between properties and attributes.

Eric Lippert
+1, Eric your blog posts are real inspiration to me, keep the great work.
Darin Dimitrov
This would probably be why I have yet to be in the position of actually writing and implementing a custom attribute..
Jimmy Hoffa