我从嵌套数据类
获取Node的代码的setter中的堆栈溢出除外如何在基类和派生类中处理嵌套数据类Desc,以便我可以在主窗口中创建的新节点中使用此数据?
namespace Lib
{
// Nested Data Class
public class Desc
{
public Desc(string shape, Nullable<bool>[] inpins)
{
this.inpins = inpins;
}
string shape { get; set; }
Nullable<bool>[] inpins { get; set; }
}
// Base class drived from ShapeNode class in vendor's framework
public class Node : ShapeNode
{
public Node()
{
}
// Make a copy of Node
public Node(Node copy)
: base(copy)
{
Text = copy.Text;
NodeId = copy.NodeId;
}
public virtual Node Clone()
{
return new Node(this);
}
// Base Constructor
public Node(string Text, Desc NodeId)
{
this.Text = Text;
this.NodeId = NodeId;
}
new public string Text { get { return base.Text; } set { base.Text = value; } }
public Desc NodeId { get { return NodeId; } set { NodeId = value; }
}
}
namespace Test
{
// Main Window code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
nodes = new Node[]
{ new A(
"TESTA",
new Desc(new Nullable<bool>[]{false, false})),
new B(
"TESTB",
new Desc(new Nullable<bool>[] {false, false, false}))
}
}
}
答案 0 :(得分:2)
属性getter(和setter)只是一个具有特殊签名和名称的方法。所以我们可以将NodeId的getter重写为:
public Desc get_NodeId()
{
// recursive call
return get_NodeId();
}
要解决此问题,请更换
public Desc NodeId { get { return NodeId; } set { NodeId = value; }}
通过
public Desc NodeId { get; set; }
在这种情况下,我们有(当此方法没有内联时):
public Desc get_NodeId()
{
// compiler-generated backing field
return _nodeId;
}
答案 1 :(得分:0)
这是一个循环并导致堆栈溢出异常
NodeId { get { return NodeId; }
使用2kay(+1)的答案或使用其他名称的私人变量
private Desc nodeID;
public Desc NodeId { get { return nodeID; } set { nodeID= value; }