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