forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebuggerAttributes.cs
More file actions
281 lines (237 loc) · 10.5 KB
/
DebuggerAttributes.cs
File metadata and controls
281 lines (237 loc) · 10.5 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace System.Diagnostics
{
internal class DebuggerAttributeInfo
{
public object Instance { get; set; }
public IEnumerable<PropertyInfo> Properties { get; set; }
}
internal class DebuggerDisplayResult
{
public string Value { get; set; }
public string Key { get; set; }
public string Type { get; set; }
}
internal static class DebuggerAttributes
{
internal static object GetFieldValue(object obj, string fieldName)
{
return GetField(obj, fieldName).GetValue(obj);
}
internal static void InvokeDebuggerTypeProxyProperties(object obj)
{
DebuggerAttributeInfo info = ValidateDebuggerTypeProxyProperties(obj);
foreach (PropertyInfo pi in info.Properties)
{
pi.GetValue(info.Instance, null);
}
}
internal static DebuggerAttributeInfo ValidateDebuggerTypeProxyProperties(object obj)
{
Type proxyType = GetProxyType(obj);
// Create an instance of the proxy type, and make sure we can access all of the instance properties
// on the type without exception
object proxyInstance = Activator.CreateInstance(proxyType, obj);
IEnumerable<PropertyInfo> properties = GetDebuggerVisibleProperties(proxyType);
return new DebuggerAttributeInfo
{
Instance = proxyInstance,
Properties = properties
};
}
internal static void CreateDebuggerTypeProxyWithNullArgument(Type type)
{
Type proxyType = GetProxyType(type);
Activator.CreateInstance(proxyType, [null]);
}
internal static DebuggerBrowsableState? GetDebuggerBrowsableState(MemberInfo info)
{
CustomAttributeData debuggerBrowsableAttribute = info.CustomAttributes
.SingleOrDefault(a => a.AttributeType == typeof(DebuggerBrowsableAttribute));
// Enums in attribute constructors are boxed as ints, so cast to int? first.
return (DebuggerBrowsableState?)(int?)debuggerBrowsableAttribute?.ConstructorArguments.Single().Value;
}
internal static IEnumerable<FieldInfo> GetDebuggerVisibleFields(Type debuggerAttributeType)
{
// The debugger doesn't evaluate non-public members of type proxies.
IEnumerable<FieldInfo> visibleFields = debuggerAttributeType.GetFields()
.Where(fi => fi.IsPublic && GetDebuggerBrowsableState(fi) != DebuggerBrowsableState.Never);
return visibleFields;
}
internal static IEnumerable<PropertyInfo> GetDebuggerVisibleProperties(Type debuggerAttributeType)
{
// The debugger doesn't evaluate non-public members of type proxies. GetGetMethod returns null if the getter is non-public.
IEnumerable<PropertyInfo> visibleProperties = debuggerAttributeType.GetProperties()
.Where(pi => pi.GetGetMethod() != null && GetDebuggerBrowsableState(pi) != DebuggerBrowsableState.Never);
return visibleProperties;
}
internal static object GetProxyObject(object obj) => Activator.CreateInstance(GetProxyType(obj), obj);
internal static Type GetProxyType(object obj) => GetProxyType(obj.GetType());
internal static Type GetProxyType(Type type)
{
CustomAttributeData cad = FindAttribute(type, attributeType: typeof(DebuggerTypeProxyAttribute));
Type proxyType = cad.ConstructorArguments[0].ArgumentType == typeof(Type) ?
(Type)cad.ConstructorArguments[0].Value :
Type.GetType((string)cad.ConstructorArguments[0].Value);
if (type.GenericTypeArguments.Length > 0)
{
proxyType = proxyType.MakeGenericType(type.GenericTypeArguments);
}
return proxyType;
}
internal static DebuggerDisplayResult ValidateFullyDebuggerDisplayReferences(object obj)
{
CustomAttributeData cad = FindAttribute(obj.GetType(), attributeType: typeof(DebuggerDisplayAttribute));
// Get the text of the DebuggerDisplayAttribute
string attrText = (string)cad.ConstructorArguments[0].Value;
string formattedValue = EvaluateDisplayString(attrText, obj);
string formattedKey = FormatDebuggerDisplayNamedArgument(nameof(DebuggerDisplayAttribute.Name), cad, obj);
string formattedType = FormatDebuggerDisplayNamedArgument(nameof(DebuggerDisplayAttribute.Type), cad, obj);
return new DebuggerDisplayResult { Value = formattedValue, Key = formattedKey, Type = formattedType };
}
internal static string ValidateDebuggerDisplayReferences(object obj)
{
CustomAttributeData cad = FindAttribute(obj.GetType(), attributeType: typeof(DebuggerDisplayAttribute));
// Get the text of the DebuggerDisplayAttribute
string attrText = (string)cad.ConstructorArguments[0].Value;
return EvaluateDisplayString(attrText, obj);
}
private static CustomAttributeData FindAttribute(Type type, Type attributeType)
{
for (Type t = type; t != null; t = t.BaseType)
{
CustomAttributeData[] attributes = t.GetTypeInfo().CustomAttributes
.Where(a => a.AttributeType == attributeType)
.ToArray();
if (attributes.Length != 0)
{
if (attributes.Length > 1)
{
throw new InvalidOperationException($"Expected one {attributeType.Name} on {type} but found more.");
}
return attributes[0];
}
}
throw new InvalidOperationException($"Expected one {attributeType.Name} on {type}.");
}
private static string FormatDebuggerDisplayNamedArgument(string argumentName, CustomAttributeData debuggerDisplayAttributeData, object obj)
{
CustomAttributeNamedArgument namedAttribute = debuggerDisplayAttributeData.NamedArguments.FirstOrDefault(na => na.MemberName == argumentName);
if (namedAttribute != default)
{
string? value = (string?)namedAttribute.TypedValue.Value;
if (!string.IsNullOrEmpty(value))
{
return EvaluateDisplayString(value, obj);
}
}
return "";
}
private static string EvaluateDisplayString(string displayString, object obj)
{
Type objType = obj.GetType();
string[] segments = displayString.Split(['{', '}']);
if (segments.Length % 2 == 0)
{
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} lacks a closing brace.");
}
if (segments.Length == 1)
{
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} doesn't reference any expressions.");
}
var sb = new StringBuilder();
for (int i = 0; i < segments.Length; i += 2)
{
string literal = segments[i];
sb.Append(literal);
if (i + 1 < segments.Length)
{
string reference = segments[i + 1];
bool noQuotes = reference.EndsWith(",nq");
reference = reference.Replace(",nq", string.Empty);
// Evaluate the reference.
object member;
if (!TryEvaluateReference(obj, reference, out member))
{
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} contains the expression \"{reference}\".");
}
string memberString = GetDebuggerMemberString(member, noQuotes);
sb.Append(memberString);
}
}
return sb.ToString();
}
private static string GetDebuggerMemberString(object member, bool noQuotes)
{
string memberString = "null";
if (member != null)
{
memberString = member.ToString();
if (member is string)
{
if (!noQuotes)
{
memberString = '"' + memberString + '"';
}
}
else if (!IsPrimitiveType(member))
{
memberString = '{' + memberString + '}';
}
}
return memberString;
}
private static bool IsPrimitiveType(object obj) =>
obj is byte || obj is sbyte ||
obj is short || obj is ushort ||
obj is int || obj is uint ||
obj is long || obj is ulong ||
obj is float || obj is double;
private static bool TryEvaluateReference(object obj, string reference, out object member)
{
PropertyInfo pi = GetProperty(obj, reference);
if (pi != null)
{
member = pi.GetValue(obj);
return true;
}
FieldInfo fi = GetField(obj, reference);
if (fi != null)
{
member = fi.GetValue(obj);
return true;
}
member = null;
return false;
}
private static FieldInfo GetField(object obj, string fieldName)
{
for (Type t = obj.GetType(); t != null; t = t.GetTypeInfo().BaseType)
{
FieldInfo fi = t.GetTypeInfo().GetDeclaredField(fieldName);
if (fi != null)
{
return fi;
}
}
return null;
}
private static PropertyInfo GetProperty(object obj, string propertyName)
{
for (Type t = obj.GetType(); t != null; t = t.GetTypeInfo().BaseType)
{
PropertyInfo pi = t.GetTypeInfo().GetDeclaredProperty(propertyName);
if (pi != null)
{
return pi;
}
}
return null;
}
}
}