Share via


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

  • Anonymous
    August 13, 2004
    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?
  • Anonymous
    August 13, 2004
    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.
  • Anonymous
    August 13, 2004
    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?
  • Anonymous
    August 13, 2004
    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.
  • Anonymous
    December 18, 2004
    Helpful For MBA Fans.
  • Anonymous
    December 27, 2004
    [http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]