在复杂类型上链接DebuggerDisplay

我有几个定义DebuggerDisplay属性的类。 我想知道是否有一种方法可以根据另一个定义一个DebuggerDisplay属性。 如果我有以下课程:

[DebuggerDisplay ("Text = {Text}")] class A { public string Text {get;set;} } [DebuggerDisplay ("Property = {Property}")] class B { public A Property {get; set;} } 

我希望看到A类的实例,因为它是在A类DebuggerDisplay属性上定义的。 而不是我在查看B类对象时将A类ToString()方法放到调试器上。

不确定我是否正确理解您的问题但尝试:

 [DebuggerDisplay("Property = {Property.Text}")] public class B { public A Property { get; set; } } 

这将显示A的Text属性。

如果需要更复杂的控件,可以使用DebuggerTypeProxyAttribute

我知道这不是“正确的编码”,但由于我无法将我的情节联系起来,我决定回到原来的方式。 只需覆盖ToString()方法即可。 然后链接是小菜一碟。

  public partial class Tld { public override string ToString() { return this.Name; } } public partial class Domain { public override string ToString() { return this.DomainName + "." +this.Tld.ToString(); } public Domain (string domain, string tld):this( domain, new Tld(tld)) { } public Domain(string domain, Tld tld):this() { this.DomainName = domain; this.Tld = tld; } } public partial class Url { public override string ToString() { return this.Scheme + "://" + this.Subdomain + this.Domain.ToString() + ((string.IsNullOrWhiteSpace(this.Path)) ? "" : this.Path); } public Url (string scheme, string subdomain, string domain, string tld, string path):this(new Tld(tld),domain, subdomain,scheme,path){} public Url(Tld tld, string domainName, string subdomain, string scheme, string path): this(new Domain(domainName, tld),subdomain,scheme,path){} public Url(Domain domain, string subdomain, string scheme, string path):this() { this.Domain = domain; this.Path = path; this.Scheme = scheme; this.Subdomain = subdomain; } } public void Domain_Create_GOOD() { Domain expected = new Domain("google","co.nz"); } 

来自https://blogs.msdn.microsoft.com/jaredpar/2011/03/18/debuggerdisplay-attribute-best-practices/ (我添加了条件编译指令)

 #if DEBUG [DebuggerDisplay("{DebuggerDisplay}")] public sealed class Student { public string FirstName { get; set; } public string LastName { get; set; } private string DebuggerDisplay { get { return string.Format("Student: {0} {1}", FirstName, LastName);} } } #endif 

这类似于Mickey Perlstein的答案(clear属性,用于格式化调试器的字符串),而无需覆盖ToString()(毕竟可能需要用于其他目的。)

源代码还有许多其他针对DebuggerDisplay的好技巧,包括一些性能注意事项。

编辑


因为这是调试代码,所以违反OOP(从外部访问私有财产)并没有那么糟糕……但我们在这里很难违反它。

 private string DebuggerString { get { StringBuilder sb = new StringBuilder(); sb.Append("Whatever you want your Parent class' Debugger Text To Say"); var properties = typeof(GroupQuote).GetProperties() //get the properties with the DebuggerDisplay attribute and our property .Where(x = > x.PropertyType.IsDefined(typeof(DebuggerDisplayAttribute)) && x.PropertyType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Any(y = > y.Name == "DebuggerString")); foreach(PropertyInfo property in properties) { object itemWithProperty = property.GetValue(this); //we have to check our property for null, otherwise trying to get its DebuggerString property will throw an exception if (itemWithProperty != null) { PropertyInfo privateDebuggerProperty = property.PropertyType.GetProperty("DebuggerString", BindingFlags.NonPublic | BindingFlags.Instance); sb.Append(privateDebuggerProperty.GetValue(itemWithProperty)as string); } } return sb.ToString(); } } 

例

在我编写的代码中我测试了这个,我的Parent类的一些属性显示DebuggerDisplay是在它不是时定义的(可能是inheritance的东西?)。 我添加了一个额外的检查,以便我们只在实际拥有它的属性上查找DebuggerString。