考虑下面的类:
[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}")]
public class FileWrapper
{
public string FileName { get; set; }
public bool IsTempFile { get; set; }
public string TempFileName { get; set; }
}
我想添加一个基于IsTempFileName
属性的调试器显示。当实例是临时文件时,我想添加字符串, TempFileName = {TempFileName,nq}
。我怎么才能做到这一点呢?
发布于 2020-03-31 21:23:49
您可以将其与扩展方法一起使用。
using System;
using System.Linq;
using System.Diagnostics;
using System.ComponentModel;
namespace ConsoleApplicationDebuggerDisplay
{
class Program
{
static void Main(string[] args)
{
MyObject o1 = new MyObject();
MyObject o2 = new MyObject();
o1.Items = new int[] { 1, 2, 3, 4 };
}
}
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class MyObject
{
[DebuggerDisplay("{Items.ToDebuggerDisplay(),nq}")]
public int[] Items { get; set; }
[DebuggerBrowsable(DebuggerBrowsableState.Never), Browsable(false)]
internal string DebuggerDisplay
{
get
{
return string.Format("{{Items={0} ...}}"
, Items.ToDebuggerDisplay()
);
}
}
}
internal static class Extensions
{
public static bool IsNull(this object o)
{
return object.ReferenceEquals(o, null);
}
public static bool IsNotNull(this object o)
{
return !object.ReferenceEquals(o, null);
}
public static string ToDebuggerDisplay<T>(this System.Collections.Generic.IEnumerable<T> items)
{
if (items.IsNull())
return "null";
return string.Format("{{Count={0}}}", items.Count());
}
}
}
https://stackoverflow.com/questions/12892825
复制相似问题