簡體   English   中英

常見的 DebuggerDisplay 屬性實現

[英]Common DebuggerDisplay attribute implementation

我正在嘗試實現 common/general DebuggerDisplay屬性,以避免為每次使用格式化它,這將簡單地顯示 class 的每個屬性。 這就是我到目前為止所取得的成就,我的擴展方法看起來像這樣

public static class DebuggerExtension
{
    public static string ToDebuggerString(this object @this)
    {
        var builder = new StringBuilder();
        foreach (var property in @this.GetType().GetProperties())
        {
            if (property != null)
            {
                var value = property.GetValue(@this, null)?.ToString();
                builder.Append($"{property.Name}: {value}, ");
            }
        }

        return builder.ToString();
    }
}

我這樣稱呼它,它基本上可以工作:

[DebuggerDisplay($"{{this.ToDebuggerString()}}")]

我的問題是:

  1. 是否有任何現有的擴展來生成DebuggerDisplay字符串? 內置的 VS 實現只是添加了返回 ToString() 的方法,這並沒有真正的幫助
  2. 我可以以某種編譯器檢查的方式調用該方法,而不是作為字符串嗎? “這個。” 顯然在屬性 arguments 中不可用。 可能有什么表情?
  3. 還有其他可能的解決方案嗎?

您是否嘗試過在擴展方法中再次使用“ToDebuggerString()”而不是“ToString()”?
你可能會感興趣。 Visual Studio 的 Natvis 調試框架教程

下面的示例可能對您有所幫助。 (它可能會丟失!)

public static class DebuggerExtension
{
    public static string ToDebuggerDisplay(this object o)
    {
        if (object.ReferenceEquals(o, null))
            return "null";
        else if (o is string)
            return string.Format("\"{0}\"", (string)o);
        else if ((o is DateTime) || (o is TimeSpan))
            return string.Format("{{{0}}}", o);
        else if (o is System.Collections.ICollection)
            return string.Format("{{Count={0}}}", ((System.Collections.ICollection)o).Count);
        else if (o is System.Collections.IEnumerable)
        {
            int nCount = 0;
            System.Collections.IEnumerator e = ((System.Collections.IEnumerable)o).GetEnumerator();
            while (e.MoveNext())
            {
                nCount++;
            }
            return string.Format("{{Count={0}}}", nCount);
        }

        Type objType = o.GetType();

        if (objType.IsPrimitive || objType.IsEnum)
            return o.ToString();
        else if (objType.IsArray)
            return string.Format("{{Count={0}}}", ((Array)o).Length);

        PropertyInfo[] propertyInfos = objType.GetProperties(BindingFlags.Instance
            | BindingFlags.Public
            | BindingFlags.GetProperty
            | BindingFlags.DeclaredOnly);

        string retVal = "{";
        int ndx = 0;
        foreach (PropertyInfo pi in propertyInfos)
        {
            if (!pi.CanRead)
                continue;

            DebuggerBrowsableAttribute attr = pi.GetCustomAttribute<DebuggerBrowsableAttribute>();
            if (!object.ReferenceEquals(attr, null) && (attr.State == DebuggerBrowsableState.Never))
                continue;

            if (ndx++ > 0)
                retVal += " ";

            retVal += string.Format("{0}={1}", pi.Name, pi.GetValue(o).ToDebuggerDisplay());

            if (ndx > 2)
            {
                retVal += " ...";
                break;
            }
        }
        retVal += "}";

        return (ndx > 0) ? retVal : o.ToString();
    }
}

用途

[DebuggerDisplay(@"{this.ToDebuggerDisplay(),nq}")]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM