C# debuggerdisplay不';t按预期显示字段值
在C# debuggerdisplay不';t按预期显示字段值,c#,visual-studio-2015,visual-studio-2017,field,debuggerdisplay,C#,Visual Studio 2015,Visual Studio 2017,Field,Debuggerdisplay,在DebuggerDisplay属性(DDBpp1或DDBpp2)中使用哪种方法并不重要。调试器下的值列始终由{byte[2]}填充。我希望DDBpp1()方法使用字符串“DDBpp”,或者DDBpp2()方法使用短值。 该问题出现在VS15/17社区下 是否可以更改调试器下的显示字段值?如果将[DebuggerDisplay({DDBpp2()}”)]放在类本身上,它将在调试器中显示字节[]移位的int16内容-对于类: 如果将Bpp实现为成员或属性,则没有什么区别,并且赋予它更多属性也没有
DebuggerDisplay
属性(DDBpp1或DDBpp2)中使用哪种方法并不重要。调试器下的值列始终由{byte[2]}填充。我希望DDBpp1()方法使用字符串“DDBpp”,或者DDBpp2()方法使用短值。
该问题出现在VS15/17社区下
是否可以更改调试器下的显示字段值?如果将
[DebuggerDisplay({DDBpp2()}”)]
放在类本身上,它将在调试器中显示字节[]
移位的int16内容-对于类:
如果将Bpp
实现为成员或属性,则没有什么区别,并且赋予它更多属性也没有帮助
public class A
{
[DebuggerDisplay("{DDBpp1()}")]
public byte[] Bpp = new byte[2];
public string DDBpp1()
{
return "DDBpp";
}
public string DDBpp2()
{
short result;
if (BitConverter.IsLittleEndian)
{
var bppCopy = new byte[2];
Bpp.CopyTo(bppCopy, 0);
Array.Reverse(bppCopy);
result = BitConverter.ToInt16(bppCopy, 0);
}
else
{
result = BitConverter.ToInt16(Bpp, 0);
}
return result.ToString();
}
}
也许把它放在课堂上可以帮助你:
[DebuggerDisplay("{DDBpp2()}", Name = "{DDBpp2()}", TargetTypeName = "{DDBpp2()}", Type = "{DDBpp2()}"]
public byte[] Bpp { get; set; } = new byte[2];
如果您仔细查看msdn文档中给出的示例,您会发现该属性只应用于类级别——但我很困惑,为什么他们当时没有将该属性限制为类
我看了一下-它适用于更多的类,您甚至可以多次使用它
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Assembly,AllowMultiple=true)]
我的猜测是VS没有在IDE的成员/属性等上实现所有可能的结果,这就是为什么它不起作用。如果多次提供该属性,则在调试器视图中只使用第一个属性:请参见我的示例并对其进行调试。是否已选中:
“如果选中“在变量窗口中显示对象的原始结构”复选框
在“工具选项”对话框中选择,然后显示调试程序
属性被忽略“
谢谢你的建议!不幸的是,您的解决方案没有解决我的问题:(,因为我有很多其他字段,所以我无法在类级别显示它们。我的解决方法是创建属性DDBpp{get{(…)return result;}}}},然后滚动到该属性而不是字段(在调试期间)。@ael您可以使用“发送反馈”在VS中投诉/报告问题-可能是在17.9.99或更高版本中得到修复。如果没有太多的道具,您可以通过将它们全部添加到类中来将它们链接起来(将计算多个{},并在调试器中显示组合字符串:
[DebuggerDisplay({CDBpp2()}{DDBpp2()}等”)]
将在类上输出它们,如答案中所述,属性应转到类。如果成员需要该属性,则成员本身应为类。事实上,如果数据太复杂而无法显示,则可能是类自己做了太多的工作。在我的VS上检查了该属性,我将其取消选中,因此它应该注意属性。+1作为提示,可以为以后的访问者设置。@Olaf,我也在VS2017下取消选中了复选框。
[DebuggerDisplay("{CDBpp2()}")]
[DebuggerDisplay("{DDBpp2()}")]
public class A
{
[DebuggerDisplay("{DDBpp2()}", Name = "{DDBpp2()}", TargetTypeName = "{DDBpp2()}", Type = "{DDBpp2()}")]
public byte[] Bpp { get; set; } = new byte[2] { 255, 255 };
public byte[] Cpp { get; set; } = new byte[2] { 11, 28 };
public string CDBpp2() => ToDebugStr(Cpp);
public string DDBpp2() => ToDebugStr(Bpp);
string ToDebugStr(byte[] b)
{
short result;
if (BitConverter.IsLittleEndian)
{
var bppCopy = new byte[2];
b.CopyTo(bppCopy, 0);
Array.Reverse(bppCopy);
result = BitConverter.ToInt16(bppCopy, 0);
}
else
{
result = BitConverter.ToInt16(b, 0);
}
return result.ToString();
}
}