Conditional Attributes


There's an extension to the conditional concept that shows up in Whidbey. It's not in Beta1 (well, to be more correct, it doesn't really work in beta 1), but it will be functional in beta2.

If you put a conditional attribute on an attribute definition, that instructs the compiler to only place that attribute in the assembly if the symbol is defined. This allows you to have "debug only" attributes that don't take up space in your release assemblies.

 

Comments (7)

  1. Roy J. Salisbury @ VsDevCentral says:

    So your saying your putting a conditional attribute on an attribute? Or the conditional attribute will be expanded in some way to do something different?

  2. Eric Gunnerson says:

    Sorry I wasn’t clearer.

    If you put [Conditional("DEBUG")] in front of an attribute class definition, that attribute will only make it into metadata if DEBUG is set during compilation.

  3. Roy J. Salisbury @ VsDevCentral says:

    OK.. I think I understand the post now..

    [Conditional("DEBUG")]

    public class MyAttribute : Attribute { .. }

    public class TestClass {

    [MyAttribute]

    public void MyMethod() { .. }

    }

    If I had "DEBUG" defined, then the attribute on "MyMethod" would not be written to the metadata.

    What is the difference between that and this (other than easier to read code):

    public class MyAttribute : Attribute { .. }

    public class TestClass {

    #if DEBUG

    [MyAttribute]

    #endif

    public void MyMethod() { .. }

    }

    I would think that the conditional define would actually make things harder to understand becuase looking at the defination of "MyMethod" I have no idea WHEN it is going to be valid .. with the #if .. #endif way I can tell just by looking at it.

    Am I missing something?

  4. Matthew W. Jackson says:

    I for one am endlessly thankful for this feature. Our team uses several attributes to keep track of a full version history for each assembly, class, and method. Any time a major modification is made, the change is documented in attributes. Also, comments on changes that need to be made are documented in attributes, so we know if a piece of code needs to be implemented, hasn’t been fully tested, needs refactoring, or has any suspected problems. Our documentation-building tool can walk the assemblies and generate a detailed change-log, as well as a list of things that still need to be done.

    However, as we don’t really want these attributes to show up in our releases (not necessarily our release build, but our complete builds of all assemblies). Now we can define a DOCUMENT define, and put a [Conditional("DOCUMENT")] on these attributes.

    Currently, we have to use a tool that removes the unwanted attributes, or wrap the attributes in #if #endif blocks (which looks ugly).

    What remains to be seen is if these attributes are localized to a single assembly, and they are the only classess referenced by another assembly, will the C# compiler will automatically drop the reference to that assembly if all of the conditions are left out for that build? This way we won’t even need to ship the central assembly that contains these attributes with our programs.

    I’ll try this out later.

  5. MBA says:

    Helpful For MBA Fans.

Skip to main content