| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Diagnostics; |
| 7 |
using System.Linq; |
| 8 |
using System.Reflection; |
| 9 |
using System.Text; |
| 10 |
|
| 11 |
namespace System.Diagnostics |
| 12 |
{ |
| 13 |
internal class DebuggerAttributeInfo |
| 14 |
{ |
| 15 |
public object Instance { get; set; } |
| 16 |
public IEnumerable<PropertyInfo> Properties { get; set; } |
| 17 |
} |
| 18 |
|
| 19 |
internal static class DebuggerAttributes |
| 20 |
{ |
| 21 |
internal static object GetFieldValue(object obj, string fieldName) |
| 22 |
{ |
| 23 |
return GetField(obj, fieldName).GetValue(obj); |
| 24 |
} |
| 25 |
|
| 26 |
internal static void InvokeDebuggerTypeProxyProperties(object obj) |
| 27 |
{ |
| 28 |
DebuggerAttributeInfo info = ValidateDebuggerTypeProxyProperties(obj.GetType(), obj); |
| 29 |
foreach (PropertyInfo pi in info.Properties) |
| 30 |
{ |
| 31 |
pi.GetValue(info.Instance, null); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
internal static DebuggerAttributeInfo ValidateDebuggerTypeProxyProperties(object obj) |
| 36 |
{ |
| 37 |
return ValidateDebuggerTypeProxyProperties(obj.GetType(), obj); |
| 38 |
} |
| 39 |
|
| 40 |
internal static DebuggerAttributeInfo ValidateDebuggerTypeProxyProperties(Type type, object obj) |
| 41 |
{ |
| 42 |
return ValidateDebuggerTypeProxyProperties(type, type.GenericTypeArguments, obj); |
| 43 |
} |
| 44 |
|
| 45 |
internal static DebuggerAttributeInfo ValidateDebuggerTypeProxyProperties(Type type, Type[] genericTypeArguments, object obj) |
| 46 |
{ |
| 47 |
Type proxyType = GetProxyType(type, genericTypeArguments); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
object proxyInstance = Activator.CreateInstance(proxyType, obj); |
| 52 |
IEnumerable<PropertyInfo> properties = GetDebuggerVisibleProperties(proxyType); |
| 53 |
return new DebuggerAttributeInfo |
| 54 |
{ |
| 55 |
Instance = proxyInstance, |
| 56 |
Properties = properties |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
public static DebuggerBrowsableState? GetDebuggerBrowsableState(MemberInfo info) |
| 61 |
{ |
| 62 |
CustomAttributeData debuggerBrowsableAttribute = info.CustomAttributes |
| 63 |
.SingleOrDefault(a => a.AttributeType == typeof(DebuggerBrowsableAttribute)); |
| 64 |
|
| 65 |
return (DebuggerBrowsableState?)(int?)debuggerBrowsableAttribute?.ConstructorArguments.Single().Value; |
| 66 |
} |
| 67 |
|
| 68 |
public static IEnumerable<FieldInfo> GetDebuggerVisibleFields(Type debuggerAttributeType) |
| 69 |
{ |
| 70 |
|
| 71 |
IEnumerable<FieldInfo> visibleFields = debuggerAttributeType.GetFields() |
| 72 |
.Where(fi => fi.IsPublic && GetDebuggerBrowsableState(fi) != DebuggerBrowsableState.Never); |
| 73 |
return visibleFields; |
| 74 |
} |
| 75 |
|
| 76 |
public static IEnumerable<PropertyInfo> GetDebuggerVisibleProperties(Type debuggerAttributeType) |
| 77 |
{ |
| 78 |
|
| 79 |
IEnumerable<PropertyInfo> visibleProperties = debuggerAttributeType.GetProperties() |
| 80 |
.Where(pi => pi.GetGetMethod() != null && GetDebuggerBrowsableState(pi) != DebuggerBrowsableState.Never); |
| 81 |
return visibleProperties; |
| 82 |
} |
| 83 |
|
| 84 |
public static object GetProxyObject(object obj) => Activator.CreateInstance(GetProxyType(obj), obj); |
| 85 |
|
| 86 |
public static Type GetProxyType(object obj) => GetProxyType(obj.GetType()); |
| 87 |
|
| 88 |
public static Type GetProxyType(Type type) => GetProxyType(type, type.GenericTypeArguments); |
| 89 |
|
| 90 |
private static Type GetProxyType(Type type, Type[] genericTypeArguments) |
| 91 |
{ |
| 92 |
|
| 93 |
var attrs = |
| 94 |
type.GetTypeInfo().CustomAttributes |
| 95 |
.Where(a => a.AttributeType == typeof(DebuggerTypeProxyAttribute)) |
| 96 |
.ToArray(); |
| 97 |
if (attrs.Length != 1) |
| 98 |
{ |
| 99 |
throw new InvalidOperationException($"Expected one DebuggerTypeProxyAttribute on {type}."); |
| 100 |
} |
| 101 |
CustomAttributeData cad = attrs[0]; |
| 102 |
|
| 103 |
Type proxyType = cad.ConstructorArguments[0].ArgumentType == typeof(Type) ? |
| 104 |
(Type)cad.ConstructorArguments[0].Value : |
| 105 |
Type.GetType((string)cad.ConstructorArguments[0].Value); |
| 106 |
if (genericTypeArguments.Length > 0) |
| 107 |
{ |
| 108 |
proxyType = proxyType.MakeGenericType(genericTypeArguments); |
| 109 |
} |
| 110 |
|
| 111 |
return proxyType; |
| 112 |
} |
| 113 |
|
| 114 |
internal static string ValidateDebuggerDisplayReferences(object obj) |
| 115 |
{ |
| 116 |
|
| 117 |
var objType = obj.GetType(); |
| 118 |
var attrs = |
| 119 |
objType.GetTypeInfo().CustomAttributes |
| 120 |
.Where(a => a.AttributeType == typeof(DebuggerDisplayAttribute)) |
| 121 |
.ToArray(); |
| 122 |
if (attrs.Length != 1) |
| 123 |
{ |
| 124 |
throw new InvalidOperationException($"Expected one DebuggerDisplayAttribute on {objType}."); |
| 125 |
} |
| 126 |
var cad = attrs[0]; |
| 127 |
|
| 128 |
|
| 129 |
string attrText = (string)cad.ConstructorArguments[0].Value; |
| 130 |
|
| 131 |
var segments = attrText.Split(new[] { '{', '}' }); |
| 132 |
|
| 133 |
if (segments.Length % 2 == 0) |
| 134 |
{ |
| 135 |
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} lacks a closing brace."); |
| 136 |
} |
| 137 |
|
| 138 |
if (segments.Length == 1) |
| 139 |
{ |
| 140 |
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} doesn't reference any expressions."); |
| 141 |
} |
| 142 |
|
| 143 |
var sb = new StringBuilder(); |
| 144 |
|
| 145 |
for (int i = 0; i < segments.Length; i += 2) |
| 146 |
{ |
| 147 |
string literal = segments[i]; |
| 148 |
sb.Append(literal); |
| 149 |
|
| 150 |
if (i + 1 < segments.Length) |
| 151 |
{ |
| 152 |
string reference = segments[i + 1]; |
| 153 |
bool noQuotes = reference.EndsWith(",nq"); |
| 154 |
|
| 155 |
reference = reference.Replace(",nq", string.Empty); |
| 156 |
|
| 157 |
|
| 158 |
object member; |
| 159 |
if (!TryEvaluateReference(obj, reference, out member)) |
| 160 |
{ |
| 161 |
throw new InvalidOperationException($"The DebuggerDisplayAttribute for {objType} contains the expression \"{reference}\"."); |
| 162 |
} |
| 163 |
|
| 164 |
string memberString = GetDebuggerMemberString(member, noQuotes); |
| 165 |
|
| 166 |
sb.Append(memberString); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return sb.ToString(); |
| 171 |
} |
| 172 |
|
| 173 |
private static string GetDebuggerMemberString(object member, bool noQuotes) |
| 174 |
{ |
| 175 |
string memberString = "null"; |
| 176 |
if (member != null) |
| 177 |
{ |
| 178 |
memberString = member.ToString(); |
| 179 |
if (member is string) |
| 180 |
{ |
| 181 |
if (!noQuotes) |
| 182 |
{ |
| 183 |
memberString = '"' + memberString + '"'; |
| 184 |
} |
| 185 |
} |
| 186 |
else if (!IsPrimitiveType(member)) |
| 187 |
{ |
| 188 |
memberString = '{' + memberString + '}'; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return memberString; |
| 193 |
} |
| 194 |
|
| 195 |
private static bool IsPrimitiveType(object obj) => |
| 196 |
obj is byte || obj is sbyte || |
| 197 |
obj is short || obj is ushort || |
| 198 |
obj is int || obj is uint || |
| 199 |
obj is long || obj is ulong || |
| 200 |
obj is float || obj is double; |
| 201 |
|
| 202 |
private static bool TryEvaluateReference(object obj, string reference, out object member) |
| 203 |
{ |
| 204 |
PropertyInfo pi = GetProperty(obj, reference); |
| 205 |
if (pi != null) |
| 206 |
{ |
| 207 |
member = pi.GetValue(obj); |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
FieldInfo fi = GetField(obj, reference); |
| 212 |
if (fi != null) |
| 213 |
{ |
| 214 |
member = fi.GetValue(obj); |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
member = null; |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
private static FieldInfo GetField(object obj, string fieldName) |
| 223 |
{ |
| 224 |
for (Type t = obj.GetType(); t != null; t = t.GetTypeInfo().BaseType) |
| 225 |
{ |
| 226 |
FieldInfo fi = t.GetTypeInfo().GetDeclaredField(fieldName); |
| 227 |
if (fi != null) |
| 228 |
{ |
| 229 |
return fi; |
| 230 |
} |
| 231 |
} |
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
private static PropertyInfo GetProperty(object obj, string propertyName) |
| 236 |
{ |
| 237 |
for (Type t = obj.GetType(); t != null; t = t.GetTypeInfo().BaseType) |
| 238 |
{ |
| 239 |
PropertyInfo pi = t.GetTypeInfo().GetDeclaredProperty(propertyName); |
| 240 |
if (pi != null) |
| 241 |
{ |
| 242 |
return pi; |
| 243 |
} |
| 244 |
} |
| 245 |
return null; |
| 246 |
} |
| 247 |
} |
| 248 |
} |