tags:

views:

115

answers:

2

I want to use the conditional attribute on a class, or more to the point, is there something that give that effect? Basically I don't want the class to be there in debug mode. I also don't want to have to wrap each call in a #if DEBUG directive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace NameSpace
{
    [Conditional("Debug")]
    public class ClassName
    {

        public ClassName()
        {
        }
    }
}
+3  A: 

No, there isn't. Conditional attributes don't make their targets disappear themselves - they just make the compiler omit users of the targets.

Eric Lippert had a post on just this sort of thing today, as it happens. Read it and see if it all makes more sense to you.

If you really need to omit the class itself in release mode, then use preprocessor directives - but you'll have to do the same for all the callers as well. What harm does it have to keep the class around in release mode, anyway?

Could this actually be a class in a different project? If so, then you could just apply the conditional attribute to all the methods, then the type wouldn't be needed in release mode, so you could avoid shipping the assembly.

Jon Skeet
So can I omit all users of the class?
Anthony D
If you put Conditional on all of the *methods* of the class, then all the calls will be removed, but the class itself will still be built.
Jon Skeet
So the instances of the class will still get created and use memory?
Anthony D
A: 

Not sure, if I have right understanding of this.

But, if you decorate all the methods with ConditionalAttribute - it all will be removed when the symbol is not present. And hence, although the class is available for use, there won't be any methods at runtime to use it for.

I am not sure, why you would want to exclude a class depending upon the symbol? Could you explain the scenario?

shahkalpesh