代码之家  ›  专栏  ›  技术社区  ›  to StackOverflow

是否存在使用DebuggerDisplayAttribute呈现对象的方法

  •  6
  • to StackOverflow  · 技术社区  · 16 年前

    我有许多类是用DebuggerDisplayAttribute修饰的。

    .NET Framework中是否存在一个方法,该方法将显示使用DebuggerDisplayAttribute格式化的对象(如果未定义DebuggerDisplayAttribute,则返回使用.ToString())?

    编辑

    为了澄清这一点,我希望框架中可能会有一些东西。我知道我可以从DebuggerDisplayAttribute获取Value属性,但是我需要使用DebuggerDisplayAttribute.Value表示的格式字符串格式化我的实例。

    如果我自己动手,我会设想一种扩展方法,大致如下:

    public string FormatDebugDisplay(this object value)
    {
        DebugDisplayAttribute attribute = ... get the attribute for value ...
        if (attribute = null) return value.ToString();
    
        string formatString = attribute.Value;
    
        ??? How do I format value using formatString ???
        return SomeFormatMethod(formatString, value);
    }
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   Joe White    15 年前

    这可能很好——但是DebuggerDisplayAttribute的格式字符串是由调试器计算的,就像它计算您在监视窗口或立即窗口中键入的表达式一样。这就是为什么你可以把任意表达式放在括号里,比如 {FirstName + " " + LastName} .

    因此,要在代码中计算这些值,需要将VisualStudio调试器嵌入到应用程序中。可能不会发生((咧嘴笑)

    最好的办法可能是采用DebuggerDisplay格式字符串中当前的所有格式逻辑,并将其作为一个方法。然后您可以从代码中自由调用该方法。DebuggerDisplay属性只会调用方法。

    [DebuggerDisplay("{Inspect()}")]
    public class MyClass {
        public string Inspect() { ... }
    }
    
        2
  •  2
  •   Kenneth LeFebvre    10 年前

    实现DebuggerDisplayAttribute在调试器中提供的功能,但这正是我在代码中使用的功能。它涵盖了我们在代码库中遇到的大约90%(或更多)的案例。如果你能把它修好覆盖更多的案例,我很乐意看到你的改进!

        public static string ToDebuggerString(this object @this)
        {
            var display = @this.GetType().GetCustomAttributes(typeof (DebuggerDisplayAttribute),false).FirstOrDefault() as DebuggerDisplayAttribute;
            if (display == null)
                return @this.ToString();
    
            var format = display.Value;
            var builder = new StringBuilder();
            for (var index = 0; index < format.Length; index++)
            {
                if (format[index] == '{')
                {
                    var close = format.IndexOf('}', index);
                    if (close > index)
                    {
                        index++;
                        var name = format.Substring(index, close - index);
                        var property = @this.GetType().GetProperty(name);
                        if (property != null)
                        {
                            var value = property.GetValue(@this, null).ToString();
                            builder.Append(value);
                            index += name.Length;
                        }
                    }
                }
                else
                    builder.Append(format[index]);
            }
            return builder.ToString();
        }
    
        3
  •  1
  •   M4N    16 年前

    Value 返回所需内容的属性。

    var attribute = obj.GetType().
        GetCustomAttributes(typeof(DebuggerDisplayAttribute), false);
    return (attribute == null) ? obj.ToString() : attribute.Value;
    

    您甚至可以将其放入扩展方法中:

    public static string ToDebugString(this object obj)
    {
        var attribute = obj.GetType().
            GetCustomAttributes(typeof(DebuggerDisplayAttribute), false);
        return (attribute == null) ? obj.ToString() : attribute.Value;
    }
    

    您可以在每个对象上调用它: myObject.ToDebugString()

    推荐文章