如何获取集合的父对象?

时间:2015-08-13 02:29:23

标签: c# collectionbase

在C#中,如果我有一个具有集合的对象,是否可以检索包含该集合的对象?

以下是一个例子:

public class TestObject
{
    public string name { get; set; }
    public TestObjectCollection testObjects{ get; set; } 
}

TestObjectCollection集合继承自CollectionBase,是TestObjects的集合。

以下是一个示例实现:

  • 创建名为TestObject
  • "Test1"
  • 名称为TestObject的{​​{1}}有一个"Test1" TestObjectCollection名称为TestObject
  • "Test2"

如果我的TestObject名称为"Test2",我怎样才能获得名为TestObject的{​​{1}}

由于

4 个答案:

答案 0 :(得分:2)

执行此操作的唯一方法是在子对象中保留对父级的引用。您可以在创建子对象时执行此操作:

this.testObjects = new TestObjectCollection(this);

然后在TestObjectCollection的构造函数中:

public TestObject ParentObject { get; set; }

public TestObjectCollection(TestObject parent)
{
    ParentObject = parent;
    ...
}

答案 1 :(得分:0)

也许你可以这样做:

gem uninstall pg
gem uninstall activerecord-postgresql-adapter
bundle install

我以List为例。

答案 2 :(得分:0)

除非你明确地编写了这种父子关系(如Yogesh的回答),否则无法找到"""父 - 很大程度上是因为可以有多个这样的父母:

public class TestObject
{
    public string name { get; set; }
    public TestObjectCollection testObjects{ get; set; } 
}
public class TestObjectCollection : CollectionBase
{
    public void Add(TestObject to)
    {
        this.List.Add(to);
    }
}
void Main()
{
    TestObjectCollection children = new TestObjectCollection();
    TestObject child = new TestObject { name = "child" };
    children.Add(child);

    TestObject parent = new TestObject { name = "parent", testObjects = children }; 
    TestObject otherParent = new TestObject { name = "otherParent", testObjects = children };   
    TestObject stepParent = new TestObject { name = "stepParent", testObjects = children }; 
    TestObject inLocoParentis = new TestObject { name = "inLocoParentis", testObjects = children };
    // and we can keep going on and on and on ...   
}

答案 3 :(得分:0)

如果您不想在构造函数中传递引用,则可以使用静态字典来跟踪TestObject实例,并让TestObjectCollection在延迟加载中从该静态字典中查找它的父级方式。

例如

public class TestObject
{
    /// <summary>
    /// Keep a list of all the instances of TestObject's that are created.
    /// </summary>
    internal static Dictionary<Guid, TestObject> _collections = new Dictionary<Guid, TestObject>();

    /// <summary>
    /// An ID to uniquely identify an instance of a TestObject
    /// </summary>
    public Guid ID { get; private set; }

    /// <summary>
    /// A reference to the collection which will be set in the constructor
    /// </summary>
    public TestObjectCollection TestObjects { get; private set; }

    public TestObject()
    {
        //generate the unique id
        this.ID = Guid.NewGuid();
        this.TestObjects = new TestObjectCollection();
        //add this testobject to the List of test objects.
        _collections.Add(this.ID, this);
    }

    /// <summary>
    /// Destructor, kill the TestObject from the list of TestObject's.
    /// </summary>
    ~TestObject()
    {
        if (_collections.ContainsKey(this.ID))
        {
            _collections.Remove(this.ID);
        }
    }
}

public class TestObjectCollection : IEnumerable<TestObject>
{
    private List<TestObject> _testObjects = new List<TestObject>();

    public Guid ID { get; private set; }

    public TestObject this[int i]
    {
        get
        {
            return _testObjects[i];
        }
    }

    private TestObject _Parent = null;
    public TestObject Parent
    {
        get
        {
            if (_Parent == null)
            {
                _Parent = TestObject._collections.Values.Where(p => p.TestObjects.ID == this.ID).FirstOrDefault();
            }
            return _Parent;
        }
    }

    public TestObjectCollection()
    {
        this.ID = Guid.NewGuid();
    }

    public void Add(TestObject newObject)
    {
        if (newObject != null)
            _testObjects.Add(newObject);
    }

    public IEnumerator<TestObject> GetEnumerator()
    {
        return _testObjects.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _testObjects.GetEnumerator();
    }
}

...测试

class Program
{
    static void Main(string[] args)
    {
        TestObject tObject = new TestObject();
        Console.WriteLine("TestObject ID: " + tObject.ID);
        Console.WriteLine("TestObject TestObjectCollection ID: " + tObject.TestObjects.ID);
        Console.WriteLine("TestObject TestObjectCollection Parent ID: " + tObject.TestObjects.Parent.ID);
        Console.WriteLine("Press any key...");

        Console.ReadKey(true);
    }
}

这样做是在TestObject的构造函数中,它本身就是一个GUID ID。然后它创建一个TestaceCollection的Instace。

在TestObjectCollection的构造函数中,它给自己一个GUID ID。

回到TestObject的构造函数中,它将TestObjects设置为它刚刚创建的集合,然后将参考添加到TestObjects的Dictionary中,这是静态的。它使用TestObject的ID作为所述词典的键。

然后在TestObjectCollection中,通过在静态字典中查找它来获取父集合,使用一个不设置自身的属性直到它被调用(因为你无法确定)它在构造函数中,因为TestObject构造函数还没有添加引用。

    private TestObject _Parent = null;
    public TestObject Parent
    {
        get
        {
            if (_Parent == null)
            {
                _Parent = TestObject._collections.Values.Where(p => p.TestObjects.ID == this.ID).FirstOrDefault();
            }
            return _Parent;
        }
    }