LP on .NET

December 4, 2009

Conditional Attributes

Filed under: .NET,C#,Software Development — Larry Parker @ 8:42 am

Just when I gave up hope that I would have to retire a handful of attributes from my source code because they contained sensitive information (e.g. the name of the code reviewer), I came across this little gem in .NET – conditional attributes.

Now those “troublesome” attributes look something like this:

public class CodeQualityAttribute : Attribute
    public String Author { get; set; }
    public String CodeReviewer { get; set; }
    public String UnitTestCompleted { get; set; }
    public Boolean OptimizationNeeded { get; set; }

By applying the Conditional attribute with “DEBUG” in the constructor, an attribute will only be compiled for debug builds — not only when it’s defined, but also when it’s used.

Now I can have the best of both worlds – sensitive information in my debug assemblies (which I can submit to a reflection utility for reporting), and clean release assemblies.

Pretty cool feature, huh?



  1. I assume the Conditional attribute only affects any attributes *following* it? So if you do want some attributes to be preserved for the production build, then you’d place them *before* the conditional attribute?

    Comment by Wayne S. — December 7, 2009 @ 11:12 am | Reply

  2. I don’t think the ordering matters. It’s all strongly typed, so the compiler will figure out that it needs to build the conditional attribute first.

    Comment by Larry Parker — December 7, 2009 @ 1:36 pm | Reply

RSS feed for comments on this post. TrackBack URI

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: