繁体   English   中英

c#是否有相当于格式化类成员的DebuggerDisplay?

[英]c# is there a DebuggerDisplay equivalent to format class members?

DebuggerDisplay 属性允许为整个类显示自定义“值”或解释。 这很好,但是是否有可能强制将标准类型成员(即 UInt32)也显示为十六进制值?

我想要这个有两个原因

  • 我的一些成员仅在十六进制中有意义(地址、位掩码)
  • 十六进制格式在 C# IDE 中是全局的,所以我必须经常手动切换

[DebuggerDisplay("{_value,h}")] 
public abstract class DataField
{
  [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  private UInt32 _value;
            
  // display this member in debugger permanent as HEX 
  public UInt32 ConstMask { get; set; } 
}

我看到的一个选择是将 ConstMask 声明为一个类并应用 DebuggerDisplay 格式,但这会影响我的性能,我想这不是一个好的选择,仅用于调试目的。

提前感谢您的提示,

当您需要更多控制/代码来格式化调试器显示时,可以使用nq和用于返回字符串的属性名称。 像这样:

[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class DataField
{
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private UInt32 _value;

    // display this member in debugger permanent as HEX 
    public UInt32 ConstMask { get; set; }

    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string DebuggerDisplay =>
        $"{this.GetType().Name} 0x{this._value:X8}";
}

您正在寻找DebuggerTypeProxyAttribute 此属性允许您定义一个可以扩展以显示所有属性的类型。

[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
    private const string TestString = "This should not appear in the debug window.";

    internal class HashtableDebugView
    {
        private Hashtable hashtable;
        public const string TestString = "This should appear in the debug window.";
        public HashtableDebugView(Hashtable hashtable)
        {
            this.hashtable = hashtable;
        }

        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
        public KeyValuePairs[] Keys
        {
            get
            {
                KeyValuePairs[] keys = new KeyValuePairs[hashtable.Count];

                int i = 0;
                foreach(object key in hashtable.Keys)
                {
                    keys[i] = new KeyValuePairs(hashtable, key, hashtable[key]);
                    i++;
                }
                return keys;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM