![](/img/trans.png)
[英]How to make DebuggerDisplay work for composite types in Dictionary?
[英]Chaining DebuggerDisplay on complex types
我有幾個定義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是在它不是時定義的(可能是繼承的東西?)。 我添加了一個額外的檢查,以便我們只在實際擁有它的屬性上查找DebuggerString。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.