【问题标题】:How to reference parent in inline creation of objects?如何在对象的内联创建中引用父级?
【发布时间】:2011-07-27 12:00:46
【问题描述】:

假设我有一个父/子关系 ob 对象并尝试内联创建一个父对象(我不太确定这是正确的词)。是否可以在自己的创建代码中引用创建的父级?

Parent = new Parent(
{
    Name = "Parent",
    Child= new Child(/*ReferenceToParent*/)
});

【问题讨论】:

    标签: c#


    【解决方案1】:

    解决这个问题的唯一方法是Parent 构造函数调用Child 构造函数本身并传入this。您的对象初始化器(我假设您正在尝试这样做)然后可以在孩子上设置其他属性:

    public class Parent
    {
        public Child Child { get; private set; }
        public string Name { get; set; }
    
        public Parent()
        {
            Child = new Child(this);
        }
    }
    
    public class Child
    {
        private readonly Parent parent;
        public string Name { get; set; }
    
        public Child(Parent parent)
        {
            this.parent = parent;
        }
    }
    

    然后:

    Parent parent = new Parent
    {
        Name = "Parent name",
        // Sets the Name property on the existing Child
        Child = { Name = "Child name" }
    };
    

    我会尝试避免这种关系 - 随着时间的推移,它会变得越来越棘手。

    【讨论】:

    • 谢谢。您能否详细说明“棘手的部分”?我的印象是垃圾收集器可以很好地处理这种循环对象引用。我可以想象序列化会出现问题,但我不打算在这里使用。
    • @Amenti:垃圾收集器很好,但是在您自己的构造函数中“泄漏”对this 的引用通常是个坏主意……如果Child 构造函数试图在其上调用任何东西its 构造函数中的父级,它将在未完全初始化的对象上运行。这可能是导致难以跟踪的细微错误的原因。
    【解决方案2】:

    您不能这样做,因为尚未创建 Parent 的实例。如果 child 在其构造函数中需要 Parent 的实例,则必须创建一个。 先创建一个Parent的实例,然后Child把parent传给Constructor,再把child的实例赋值给Parent上的属性。

    var parent = new Parent
    {
      Name = "Parent",
      //More here...
    };
    var child = new Child(parent);
    parent.Child = child;
    

    【讨论】:

      【解决方案3】:

      不,因为引用开始引用分配的对象构造函数的执行完成之后。

      【讨论】:

        【解决方案4】:

        这已经很老了,看起来在较新的 C# 版本中没有新的解决方案,是吗?如果有,请分享。

        同时,我想添加另一种类似于已接受但不同的解决方案。它假定您可以更改 Parent 类。

        using System;
        public class Program
        {
            public static void Main()
            {
                Parent p = new Parent()
                {
                    Name = "Parent",
                    Child = new Child()
                };
                
                Console.WriteLine(p.Child.Parent.Name);
            }
                
            public class Parent
            {
                public string Name {get; set;}
                
                public Child Child {
                    get { return this._child; }
                    set { 
                        this._child = value; 
                        if(value != null) 
                            value.Parent = this;
                    }
                }
                private Child _child;
            }
            
            public class Child 
            {
                public Parent Parent {get; set;}
            }  
        }
        

        可以在this link中执行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-06
          • 1970-01-01
          • 2010-12-04
          • 2020-11-01
          • 1970-01-01
          相关资源
          最近更新 更多