Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a Debug-only method in .NET?

Tags:

.net

debugging

Is it possible to create a method which performs debugging assistance like the System.Diagnostics.Debug class?

I'm looking for a way to construct a method which when called by an assembly compiled with the DEBUG conditional compilation symbol defined, results in an operation, and which is a no-op when called by an assembly without the symbol defined.

If possible, I'd like it to be possible for the calls to the debugging methods to add minimal overhead or increase in size to the release version of the assembly.

To clarify, the debugging methods should be in an assembly compiled in Release mode. Calls to the methods should only generate operations when called from an assembly with the DEBUG symbol defined in the scope of the method call.

like image 866
Paul Turner Avatar asked Sep 17 '09 15:09

Paul Turner


People also ask

How do I debug a .NET program?

Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu. Enter a string in the console window when the program prompts for a name, and then press Enter . Program execution stops when it reaches the breakpoint and before the Console.

What is debug mode in C#?

Debug mode allow developers to break the execution of program using interrupt 3 and step through the code. Debug mode has below features: Less optimized code. Some additional instructions are added to enable the developer to set a breakpoint on every source code line.

How do I make debug mode in Visual Studio?

In Solution Explorer, right-click the project and choose Properties. In the side pane, choose Build (or Compile in Visual Basic). In the Configuration list at the top, choose Debug or Release. Select the Advanced button (or the Advanced Compile Options button in Visual Basic).

What is difference between debug and release in Visual Studio?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.


3 Answers

Add the Conditional attribute to the method, like this:

[Conditional("DEBUG")]
public void Whatever() {
    //...
}

Note that the method must return void, and cannot have any out parameters; otherwise, it would be impossible to remove a call to it.

The method will be compiled into the assembly, but CLS-compliant compilers will only emit calls to the method if the assemblies that they are compiling has DEBUG defined. Note that the C++ compiler is not CLS-compliant and will always emit the call.

like image 148
SLaks Avatar answered Oct 12 '22 23:10

SLaks


ConditionalAttribute

BTW the code for the method being called remains in the assembly -- it's the calls to it that are removed at compilation time

Bonus topical blog post: http://blogs.msdn.com/ericlippert/archive/2009/09/10/what-s-the-difference-between-conditional-compilation-and-the-conditional-attribute.aspx

like image 37
Ruben Bartelink Avatar answered Oct 12 '22 22:10

Ruben Bartelink


If you disassemble System.Diagnostics.Debug class using Reflector you can see that this is done using the [Conditional("DEBUG")] attribute:

public sealed class Debug
{
    private Debug();
    [Conditional("DEBUG")]
    public static void Assert(bool condition);
    // etc...
}
like image 39
Justin Avatar answered Oct 12 '22 21:10

Justin