How do I make a field a debugger - in C # only?

I am having a problem with the same instance of a C # class being used in multiple places. I would like to tell right away in the debugger which instance is, so I added a field titled DebugUID

like this:

public readonly string DebugUID = Guid.NewGuid().ToString();

      

and I added an attribute [DebuggerDisplay("DebugUID= {DebugUID}")]

to the class.

I want the compiler to ignore the DebugUID field in release mode. If it was a method, I would add [Conditional("Debug")]

before it, but it won't let me do this for fields. How do I make a field a debugger in C # only?

+2


source to share


4 answers


C # has conditional compilation functions similar to those of C / C ++. You can use the #if

compilation directive operator , for example DEBUG

, to accomplish this.

#if DEBUG
public readonly string DebugUID = Guid.NewGuid().ToString();
#endif

      



Since by default only Debug configurations define a directive DEBUG

, it will only compile in debug mode. In Release mode, the code inside #if

will be ignored.

+13


source


Can you suggest an alternative that involves using a debugger instead of writing code?



Right-click the instance in the viewport and select Assign Object ID. This object will now appear with this ID during a debugging session. Repeat for other instances of the same class that interest you.

+4


source


Try using the #if preprocessor directive:

class Value {

#if DEBUG
private readonly string DebugUID = Guid.NewGuid().ToString()
#endif

}

      

You need to make sure the DEBUG flag is set to debug builds (and certainly not for releases).

+1


source


You can use a conditional preprocessor including

#if DEBUG
private readonly string debugId = Guid.NewGuid().ToString();
#endif

      

+1


source







All Articles