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?
source to share
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.
source to share
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.
source to share