本文介绍了可能访问子级“调试器显示”属性属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前状态



有两个课程:

 code> [DebuggerDisplay(@One = {One},two = {Two})] 
public class A
{
public int One {get;组; }
public B Two {get;组; }
}

[DebuggerDisplay(@Three = {Three})]
public class B
{
public int Three {get;组; }
}

使用它们:

  var a = new A {One = 5,Two = new B {Three = 10}}; 

调试器内的工具提示值显示在 a

我知道这可以通过覆盖类 B ToString()方法来实现。这只是感觉不正确,因为我正在我的应用程序中编写代码进行调试。



我也知道使用类似于

$ b $的字符串b

  [DebuggerDisplay(@One = {One},two ='Three = {Two.Three}')] 

也可以工作。这也不适合我,因为它将需要类 A 具有类 B 的知识。 p>

我想要更多的方法来注入的类型的实例$ c> A



问题



是否可以访问$$code> DebuggerDisplay 属性的 DebuggerDisplay 属性的 DebuggerDisplay 属性的has-a课程?



更新



可能我的要求是不可能的, a href =http://stackoverflow.com/a/258961/107625>此SO答案。也许一个很好的解决方案是在课程 B 中覆盖 ToString ,并执行一些如果.. else 并使用只在调试器内部行为不同。



如下所示:

  [DebuggerDisplay(@Three = {Three})] 
public class B
{
public int Three {get;组; }

public override string ToString()
{
if(Debugger.IsAttached)
{
return string.Format(@Three = {0 },三);
}
else
{
return base.ToString();
}
}
}


解决方案

从OP复制可能的解决方案



可能我的要求是不可能的,因为这个。也许一个很好的解决方案是覆盖B类中的ToString,并执行一些if..else,并使用只在调试器内部行为不同。



如下:

  [DebuggerDisplay(@Three = {Three})] 
public class B
{
public int Three {get;组; }

public override string ToString()
{
if(Debugger.IsAttached)
{
return string.Format(@Three = {0 },三);
}
else
{
return base.ToString();
}
}
}


Current state

Having two classes:

[DebuggerDisplay(@"One = {One}, two = {Two}")]
public class A
{
    public int One { get; set; }
    public B Two { get; set; }
}

[DebuggerDisplay(@"Three = {Three}")]
public class B
{
    public int Three { get; set; }
}

Using them:

var a = new A {One = 5, Two = new B {Three = 10}};

Inside the debugger, the tool tip value that is displayed at a is

Goal

What I would want is something like

I know this could be achieved by overriding the ToString() method of class B. This just feels not right, since I'm writing code in my application for debugging only.

I also know that using a string similar to

[DebuggerDisplay(@"One = {One}, two = 'Three = {Two.Three}'")]

would work, too. This also does not feel right to me, since it would require that class A has knowledge of class B.

I would like to have more of a way to "inject" the value of DebuggerDisplay of type B to the instance of that type in class A.

Question

Is it somehow possible to access the DebuggerDisplay attribute of a member inside the DebuggerDisplay attribute of a "has-a" composited class?

Update

Probably, my requirement is not possible as per this SO answer. Maybe a good solution would be to override ToString in class B and do some if..else and use the Debugger.IsAttached property to behave different only inside the debugger.

Something like:

[DebuggerDisplay(@"Three = {Three}")]
public class B
{
    public int Three { get; set; }

    public override string ToString()
    {
        if (Debugger.IsAttached)
        {
            return string.Format(@"Three = {0}", Three);
        }
        else
        {
            return base.ToString();
        }
    }
}
解决方案

Copied possible solution from OP

Probably, my requirement is not possible as per this SO answer. Maybe a good solution would be to override ToString in class B and do some if..else and use the Debugger.IsAttached property to behave different only inside the debugger.

Something like:

[DebuggerDisplay(@"Three = {Three}")]
public class B
{
    public int Three { get; set; }

    public override string ToString()
    {
        if (Debugger.IsAttached)
        {
            return string.Format(@"Three = {0}", Three);
        }
        else
        {
            return base.ToString();
        }
    }
}

这篇关于可能访问子级“调试器显示”属性属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:42