3
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 社区下。

是否可以在调试器下更改显示字段值?

4

2 回答 2

3

如果您放置[DebuggerDisplay("{DDBpp2()}")]类本身,它将bytes[]在调试器中显示移位的 int16 内容 - 对于该类:

类上的 DebuggerDisplayAttribute

如果您实现Bpp为成员或属性没有区别,并且给它更多属性也无济于事。

    [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 文档中的给定示例,您会发现该属性仅适用于类级别 - 但我很困惑,为什么他们当时没有将属性限制为类。

我查看了debuggerDisplayAttribute.cs 的来源——它适用于更多类,甚至可以多次使用。

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

我的猜测是 VS 没有在成员/属性/等上实现所有可能的结果。对于 IDE,这就是它不起作用的原因。如果您多次提供该属性,则在调试器视图中仅使用第一个:请参阅我的示例并对其进行调试。

于 2017-11-25T18:01:01.127 回答
3

你检查过:

“如果在工具选项对话框中选中了在变量窗口中显示对象的原始结构复选框,则忽略 DebuggerDisplay 属性”

于 2017-11-25T20:44:20.653 回答