forked from ahotko/c-sharp-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebuggingSample.cs
161 lines (140 loc) · 5.31 KB
/
DebuggingSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#if !DEBUG
#define RELEASE
#else
#define WERE_OK
#endif
#if DEBUG
#warning This is good as it is run in Debug mode
#endif
#if (DEBUG && WERE_OK)
#undef WERE_OK
#endif
#if !DEBUG
#error This should only be run in Debug mode
#endif
using System;
using System.Diagnostics;
namespace CodeSamples.Attributes
{
sealed class FullName
{
public FullName(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
/// <summary>
/// <para>
/// DebuggerBrowsableState.Collapsed - This signifies that the default behaviour should be used for the decorated member and
/// gives the equivalent results as when the attribute is omitted. When viewed in a debugging tool the member is visible
/// and can be expanded to allow access to any further members that it contains.
/// </para>
/// <para>
/// DebuggerBrowsableState.Never - This indicates that the member should not be displayed in debugging windows. The member is
/// hidden from view completely.
/// </para>
/// <para>
/// DebuggerBrowsableState.RootHidden - This signifies that the member should not be visible but that its own members should be.
/// The members of the hidden item appear as if they were one level higher in the hierarchy of values. This setting is useful
/// for members that are used only to store structured information, such as collection types or some data objects.
/// </para>
/// </summary>
sealed class DebuggerExamplesDebuggerBrowsable
{
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public string StandardProperty { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string HiddenInDebuggerWindow { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public FullName OnlyPropertiesOfClassVisibleInDebuggerWindow { get; set; }
}
[DebuggerDisplay("StringValue = {StringValue}, IntValue = {GetIntValue()}")]
sealed class DebuggerExamplesDebuggerDisplay
{
public string StringValue { get; set; } = "SomeStringValue";
public int GetIntValue()
{
return 5 * 2;
}
/// <summary>
/// Also see <see cref="ConditionalSample"/> class
/// </summary>
[Conditional("DEBUG")]
public void ExecuteOnlyInDebugMode()
{
Console.WriteLine("Executed in DEBUG mode");
}
/// <summary>
/// Also see <see cref="ConditionalSample"/> class
/// </summary>
[Conditional("RELEASE")]
public void ExecuteOnlyInReleaseMode()
{
Console.WriteLine("Executed in RELEASE mode");
}
#if DEBUG
public void CompileOnlyWhenDebugDefined()
{
Console.WriteLine("Compiled and Executed in DEBUG mode");
}
#endif
#if !DEBUG
public void CompileOnlyWhenDebugNotDefined()
{
Console.WriteLine($"Compiled and Executed in RELEASE mode");
}
#endif
public void RuntimeDebuggerAttached()
{
string debuggerAttached = Debugger.IsAttached ? "is" : "is not";
Console.WriteLine($"Runtime Debugger Checking: Debugger {debuggerAttached} attached");
Console.WriteLine($"Assuming we're running {(Debugger.IsAttached ? "from IDE" : "standalone")}");
}
[DebuggerStepThrough]
public void DebuggerStepThroughMethod()
{
Console.WriteLine($"Debugger stepped through this method.");
}
/// <summary>
/// Trace and debug messages
/// This shows in Immediate Window (Ctrl + Alt + I)
/// e.g. Input in Immediate window (after build): ?(new DebuggingSample().Execute())
/// </summary>
public void ImmediateWindowMessages()
{
Trace.WriteLine("Some Trace messages to follow:");
Trace.IndentSize = 2;
Trace.Indent();
Trace.TraceInformation("This is Trace information.");
Trace.Indent();
Trace.TraceInformation("This is Trace information - indented.");
Trace.Unindent();
Trace.TraceWarning("This is Trace Warning.");
Trace.TraceError("This is Trace Error.");
Trace.Unindent();
Trace.Flush();
//
Debug.WriteLine("This is Debug Line.");
}
}
public class DebuggingSample : SampleExecute
{
public override void Execute()
{
Title("DebuggingSampleExecute");
Section("Creating class (if breakpoint is used here, debugger output will be formatted as in DebuggerDisplay())");
DebuggerExamplesDebuggerDisplay debuggerExample = new DebuggerExamplesDebuggerDisplay();
//set breakpoint on next line and hover over "debuggerExample" variable
debuggerExample.DebuggerStepThroughMethod();
debuggerExample.ExecuteOnlyInDebugMode();
debuggerExample.ExecuteOnlyInReleaseMode();
debuggerExample.RuntimeDebuggerAttached();
Section("Immediate Window Messages");
debuggerExample.ImmediateWindowMessages();
Finish();
}
}
}