Getting started with attributes in .NET – part III

After another long blogging pause (for a good cause, I must say!), it’s time to go back to our .NET attribute series. In the previous post, we’ve starting looking at how we can build new custom attributes. We’ve already met several restrictions (ex.: inherit from the Attribute base class, limitations applied to the types used as fields, etc), but there’s still one important thing to talk about: how to restrict the members to which an attribute is applied.

By default, you can apply attributes to any member type. This behavior can be controlled through the usage of the AttributeUsageAttribute class. This class introduces several properties which are used by the compiler to check for the proper usage of a custom attribute. The next snippet illustrates its use:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DumbAttribute:Attribute {

From now on, we can only apply the DumbAttribute to methods and classes. It’s no longer possible to annotate an interface with this attribute. As you can see, the AttributeTargets enum is a flags enum (notice the combination of values) which introduces several constants that identify the application elements to which the custom attribute can be applied to. Currently, this is the only positional parameter that is expected by the AttributeUsageAttribute class. There are, however, several named parameters which are important:

  • AllowMultiple: expects a Boolean which indicates that the custom attribute may be applied more than once to an element;
  • Inherited: passing true means that the custom attribute will he inherited by derived classes or overriding members.

So, if you need to ensure that a custom attribute is applied only once to a member, you can something like this:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class DumbAttribute:Attribute {

And that’s it. From its use, it’s safe to assume that you’ve probably noticed that AttributeUsageAttribute extends the Attribute class. What you’re probably wondering is what happens when you don’t annotate a custom attribute with an AttributeUsageAttribute instance…well, in this case, you’ll end up with a custom attribute which can be applied once to any type of element and it will also be automatically inherited (and yes, that means that you didn’t really need to explicitly set AllowMultiple to false in the previous snippet).

And I guess it’s all for now. We’ve already covered all the basics associated with custom attribute definition and usage. In the next posts, we’ll see how we can detect their use. Stay tuned for more.

Advertisements

~ by Luis Abreu on July 4, 2011.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: