c# - 字节失败的自定义 DebuggerDisplayAttribute 为 0
问题描述
我想在 Visual Studio Code 调试器中以 0x00 格式显示字节。
我尝试了以下方法:
[assembly: DebuggerDisplay("0x{m_value.ToString(\"X2\"),nq}", Target = typeof(byte))]
它适用于字节值非零的情况。
该数组new byte[] { 0xDF, 0x86, 0x41, 0xA8, 0x00 }
在调试器中显示为:
鉴于这y
是"0x00"
之后:
byte x = 0x00;
var y = $"0x{x:X2}";
我对发生了什么感到困惑?
非常感谢人们可以提供的任何帮助。
更新
这不是我的目标,但它已经足够好了,而且不太老套。我已经使用DebuggerTypeProxy
了一个简单的视图类byte
,byte[]
它显示为:
解决方案
我提出的能够很好地实现我想要的解决方案是利用DebuggerTypeProxy
属性而不是DebuggerDisplay
属性。
我使用了以下内容:
[assembly: DebuggerTypeProxy(typeof(ByteArrayHexView), Target = typeof(byte[]))]
[assembly: DebuggerTypeProxy(typeof(ByteHexView), Target = typeof(byte))]
public class ByteArrayHexView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private byte[] array;
public string Hex => String.Join(", ", array.Select(x => $"0x{x:X2}"));
public ByteArrayHexView(byte[] array)
{
this.array = array;
}
}
public class ByteHexView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private byte value;
public string Hex => $"0x{value:X2}";
public ByteHexView(byte value)
{
this.value = value;
}
}
推荐阅读
- firebase - Firebase 和 Firestore - 将多个应用程序分组到一个项目中 - 身份验证如何工作?
- python - Python 3.6 相当于 md5 digest() 方法?
- performance - 最佳缓冲区大小以避免最近 i7 / i9 CPU 的缓存未命中
- javascript - 在使用函数之前验证函数的所有输入是否是最佳实践?
- c# - NuGet 更新后的程序集冲突
- vb.net - 如何使用复选框将 2 个表加入 DataGridView 并将 DGV 的状态保存回数据库
- python - 如何在Python中的嵌套字典中获取层次结构下的项目?
- python - jupyter-notebook:循环打印精细表格
- javascript - 未捕获的类型错误:无法在 HTMLButtonElement.ctrlAddItem 的 Object.addItem (app.js:41) 处读取未定义的属性“长度”
- service - Helm 部署与服务