Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Defining and Using Custom Attribute Classes in C# : Page 3

.NET has made flexible, loosely-coupled "declarative" programming available through Attributes. .NET also allows you to create your own custom Attribute classes.




Application Security Testing: An Integral Part of DevOps

Creating a Custom Attribute Class
Now we'll create a more realistic implementation of the ideas presented above. Let's create a custom attribute class. This will allow us to store some tracking information about code modifications that you would typically record as comments in source code. For the example we'll record just a few items: defect id, developer id, the date of the change, the origin of the defect, and a comment about the fix. To keep the example simple we'll focus on creating a custom attribute class (DefectTrackAttribute) designated for use only with classes and methods.

Listing 1 shows the source code for the DefectTrackAttribute class. You can identify some important lines of code.

If you haven't used attributes before, the following line of code might look a bit strange.

[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]

This line attaches an [AttributeUsage] attribute to the attribute class definition. Square bracket syntax identifies the construct as an attribute. So, Attributes classes can have their own attributes. This may seem a bit confusing at first, but it should become clearer as I show you what you'll use it for.

The [AttributeUsage] attribute has one positional parameter and two named parameters. The positional parameter validon specifies which of the various assembly types you can attach this attribute to. The value for this parameter uses a combination of values from the AttributeTargets enumeration. In my example I allow only classes and methods so I get the proper specification by OR'ing the two AttributeTargets values together.

The first named parameter of the [AttributeUsage] attribute (and the only one specified in the example) is the AllowMultiple parameter, which indicates whether you can apply this type of attribute multiple times to the same type. The default value is false. However, you want to apply the AllowMultiple parameter of the Attribute more than once on a single type because that represents what the example will model. A given method or class potentially goes through many revisions during its lifetime and you need to be able to denote each of these changes with an individual [DefectTrack] attribute.

The second named parameter of the [AttributeUsage] attribute is the Inherited parameter, which indicates whether or not derived classes inherit the attribute. I've made the default value for this parameter false. I opted to take the default value so I did not specify this named parameter. Why? The source code modification information I want to capture is always related to each class and method individually. It would confuse the developer for a class to inherit the [DefectTrack] attribute(s) from its parent class—the developer couldn't distinguish which [DefectTrack] attributes came from the parent and which were specified directly.

Listing1 then lists the class declaration. Attribute classed are subclassed from System.Attribute. You will directly or indirectly subclass all custom Attribute classes from System.Attribute.

Next, Listing 1 shows that I've defined five private fields to hold the values for the attribute.

The first method in our Attribute class is the class constructor, which has a call signature with three parameters. The parameters of a constructor for an Attribute class represent the positional parameters for that attribute, which makes these required parameters. If you choose, you can create overloaded constructors and have more than one allowable positional / required parameter configuration.

The remainder of the Attribute class is a series of public property declarations that correspond to the private fields of the class. You'll use these properties to access the values of the attribute when you get to the example that examines the metadata. Note that the properties that correspond to the positional parameters only have a get clause and do not have a set clause. This makes these properties read-only and corresponds with the fact that these are meant to be positional and not named parameters.

Applying the Custom Attribute
You've already seen that you can attach an attribute to a target item in your C# code by putting the attribute name and its parameters in square brackets immediately before the item's declaration statement.

In Listing 2 you attach the [DefectTrack] attribute to a couple of methods and a couple of classes.

You need to ensure that you have access to the class definition for your custom attribute so you start by including this line.

using MyAttributeClasses ;

Beyond that you're simply "adorning" or "decorating" your class declarations and some of your methods with the [DefectTrack] custom attribute.

SomeCustomPricingClass has two uses of the [DefectTrack] attribute attached. The first [DefectTrack] attribute uses only the three positional parameters whereas the second [DefectTrack] attribute also includes a specification for the named parameter Origin.

[DefectTrack( "1377", "12/15/02", "David Tansey" ) ] [DefectTrack( "1363", "12/12/02", "Toni Feltman", Origin = "Coding: Unhandled Exception")] public class SomeCustomPricingClass { ... }

The PriceIsValid() method also uses the [DefectTrack] custom attribute, and it includes a specification for both of the named parameters, Origin and FixComment. Listing 2 contains a couple of additional uses of the [DefectTrack] attribute that you can examine on you own.

Some readers might wonder if you could rely on the old fashioned approach of using comments for this sort of source modification information. .NET does make tools available for using XML blocks within comments to give them some structure and form.

You can easily see a comment in your source code right at the relevant place. You could process such information by text parsing the comments in the source, but its tedious and potentially error prone. .NET provides tools to process XML blocks in comments that practically eliminate this issue.

The open nature of custom attributes makes it likely that some of their most novel and powerful uses have yet to be conceived of.
Using a custom attribute for the same purpose also provides you a structured approach to recording and processing the information, but it has an added advantage. Consider that after you compile source code into a binary, you lose your comments—forever removed from the byproduct executable code. By comparison, the values of the attributes become a part of the metadata that you've forever bound to the assembly—you have still have access to the information even without any source code.

Additionally, the way an attribute "reads" in source code allows it to still fill the same valuable design-time function that the original comment did.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date