Can Visual Studio 2008 do conditional compilation for C #?

Referring to the “Configuration Manager” in the “Build” menu,

Is there a way to comment on my C # code so that the commented out code does not compile while the solution is in debug mode, but compiles if I switch to release mode?

Why do I need it? The reason I want to have code that will be compiled in Release mode, but not in Debug, is because I have code that will not work from my developer's PC (code that sends emails from my host and etc.).

Instead of going over my code and uncommenting it before posting, I would like it to be automatic.

+5
source share
4 answers

Perhaps you are looking for something like this:

#if DEBUG
     Console.WriteLine("Debug Mode");
#else
     Console.WriteLine("Release Mode");
#endif

If only the release mode is important to you, you can use:

#if !DEBUG
     Console.WriteLine("Release Mode");
#endif
+10
source

For this purpose, you can use the Conditional attribute in methods (but not in separate lines of code).

For example, the following will be compiled only in DEBUG assemblies.

[Conditional("DEBUG")]
public void MyMethod()
{
    // Do Stuff
}

The DEBUG symbol is already specified in the project settings. You must create your own symbol to build the release, say "RELEASE" so you can do this:

[Conditional("RELEASE")]
public void MyMethod()
{
    // Do Stuff
}

However, I would recommend stepping back and looking at your problem again from a higher level, since I would not highly recommend this solution.

+10

- . , , Debug.

- :

public class MyClass {

    public MyClass(IDoOtherStuff stuffToDo) {
        DoOtherStuff = stuffToDo;
    }

    private IDoOtherStuff DoOtherStuff { get; set; }

    public void Do() {
        DoOtherStuff.BeforeDo();

        // Blah blah blah..

        DoOtherStuff.AfterDo();
    }
}

public interface IDoOtherStuff {
    void BeforeDo();
    void AfterDo();
}

public class DebugOtherStuff : IDoOtherStuff {
    public void BeforeDo() {
        Debug.WriteLine("At the beginning of Do");
    }

    public void AfterDo() {
        Debug.WriteLine("At the end of Do");
    }
}

public class ReleaseOtherStuff : IDoOtherStuff {
    public void BeforeDo() { }
    public void AfterDo() { }
}

Inversion of control, , Unity, Ninject Spring.NET, Release.

+4

, , , . , .NET Reflector, , , , .

BlueMonkMN .

, ( ) , PostSharp. , .

: - . - print trace.write , , .

You can configure PostSharp to create this additional debugging information dynamically! Several configuration settings, and you can have each call to each function printed and the result (with variable contents) from each call. This makes it easy to follow the logic of the program.

-3
source

All Articles