I'm trying to implement the common/general DebuggerDisplay attribute to avoid formatting it for every single usage which would simply show every property of the class. That's what I've achieved so far, my extension method looks like this
public static class DebuggerExtension
{
public static string ToDebuggerString(this object @this)
{
var builder = new StringBuilder();
foreach (var property in @this.GetType().GetProperties())
{
if (property != null)
{
var value = property.GetValue(@this, null)?.ToString();
builder.Append($"{property.Name}: {value}, ");
}
}
return builder.ToString();
}
}
and I'm calling it like this and it basically works:
[DebuggerDisplay($"{{this.ToDebuggerString()}}")]
My questions are:
- Is there any existing extension to generate the DebuggerDisplay string? The built in VS implementation just adds the method which returns ToString() which doesn't really help
- Can I call the method on some compiler-checked way, not as a string? "this." is obviously not available in the attribute arguments. Maybe any expression?
- Any other possible solutions?