Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Conditional attribute at the class level?

Tags:

c#

.net

debugging

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()
        {
        }
    }
}
like image 454
Anthony D Avatar asked Sep 11 '09 19:09

Anthony D


People also ask

What is conditional Attributes?

A conditional attribute is a tag used to mark a method or class whose execution depends on the definition of preprocessing identifier. A conditional attribute indicates a condition to specify conditional compilation wherein methods are selectively called on the basis of definition of symbols.


1 Answers

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.

like image 69
Jon Skeet Avatar answered Oct 14 '22 16:10

Jon Skeet