c#是否有一个DebuggerDisplay等价于格式化类成员?



DebuggerDisplay属性允许显示自定义的"值";或者整个班级的翻译。这很好,但是是否有可能强制显示标准类型成员(即。UInt32)到十六进制值?

有两个原因我想要这个

  • 我的一些成员只有十六进制的意义(地址,位掩码)
  • 十六进制格式在c# IDE中是全局的,所以我必须经常手动切换

[DebuggerDisplay("{_value,h}")] 
public abstract class DataField
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private UInt32 _value;

// display this member in debugger permanent as HEX 
public UInt32 ConstMask { get; set; } 
}

我看到的一个选项是将ConstMask声明为一个类,并应用DebuggerDisplay格式,但这将影响我的性能,我想这不是一个很好的选择,只是为了调试的目的。

提前感谢你的提示,

当您需要更多的控制/代码来格式化调试器显示时,您可以使用nq和用于返回字符串的属性名称。这样的:

[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class DataField
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private UInt32 _value;
// display this member in debugger permanent as HEX 
public UInt32 ConstMask { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay =>
$"{this.GetType().Name} 0x{this._value:X8}";
}

您正在寻找DebuggerTypeProxyAttribute。此属性允许您定义一个类型,该类型可以展开以显示所有属性。

[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
private const string TestString = "This should not appear in the debug window.";
internal class HashtableDebugView
{
private Hashtable hashtable;
public const string TestString = "This should appear in the debug window.";
public HashtableDebugView(Hashtable hashtable)
{
this.hashtable = hashtable;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Keys
{
get
{
KeyValuePairs[] keys = new KeyValuePairs[hashtable.Count];
int i = 0;
foreach(object key in hashtable.Keys)
{
keys[i] = new KeyValuePairs(hashtable, key, hashtable[key]);
i++;
}
return keys;
}
}
}
}

最新更新