2
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属性(DDBpp1或DDBpp2)中使用哪种方法并不重要。调试器下的值列始终由{byte [2]}填充。我期望DDBpp1()方法的字符串“DDBpp”或DDBpp2()方法的短值。 此问题出现在VS15/17社区中。debuggerdisplay不按预期显示字段值

是否可以在调试器下更改显示字段值?如果实现Bpp作为成员或财产

DebuggerDisplayAttribute on class

这没有什么区别:该类 -

+0

正如答案中已经解释的那样,这个属性应该去上课。如果你需要一个会员,那么你的会员应该是一个班级。事实上,如果数据太复杂而无法显示,那么你的班级本身可能会做很多工作。 – Phil1970

回答

2

如果你把[DebuggerDisplay("{DDBpp2()}")]类本身,它会显示bytes[]转移INT16在调试器中的内容,并给它更多的属性也没有帮助。

[DebuggerDisplay("{DDBpp2()}", Name = "{DDBpp2()}", TargetTypeName = "{DDBpp2()}", Type = "{DDBpp2()}"] 
    public byte[] Bpp { get; set; } = new byte[2]; 

也许把它的类可以帮助你:

[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(); 
    } 
} 

如果你有MSDN文档上给出的例子仔细看,你会看到属性永远只适用在课堂上 - 我很难过,为什么他们没有把属性限制在课堂上。

我看了一下source of debuggerDisplayAttribute.cs - 它适用于更多的类,你甚至可以使用多次。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Assembly, AllowMultiple = true)]

将是VS不落实成员/属性/等所有可能的结果。对于IDE,这就是为什么它不起作用。如果多次提供属性,则只有第一个用在调试器视图中:请参阅我的示例并对其进行调试。

+0

谢谢您的建议!不幸的是你的解决方案不能解决我的问题:(因为我有很多其他领域,所以我不能在类级别显示它们。我的解决方法是创建属性DDBpp {get {(...)return result; }}然后滚动到该属性而不是字段(在调试过程中) – ael

+0

@ael您可以使用VS中的“发送反馈”来抱怨/报告问题 - 也许它在17.9.99左右得到修复。没有多少道具可以通过全部添加它们来链接它们(多个{}将被评估,并且组合的字符串显示在调试器中:'[DebuggerDisplay(“{CDBpp2()} {DDBpp2()} etc etc”)] '会在课上输出全部 –

2

你检查:

“如果框 在工具选项对话框中选择变量窗口对象的显示原料结构检查,然后DebuggerDisplay 属性被忽略”

+0

检查我的VS和我没有选中它,所以它应该注意属性。+ 1的提示​​,可以为后来的访问者设置。 –

+0

@Olaf,我没有选中复选框,也在VS2017下。 – ael