3

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

这是一个例子:

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

TestObjectCollection集合继承自CollectionBase并且是 的集合TestObjects

这是一个示例实现:

  • ATestObject的创建名称为"Test1"
  • TestObject有一个名字的有"Test1"一个TestObjectCollectionTestObject一个名字的"Test2"

如果我有TestObject名字为 的"Test2",我怎样才能得到TestObject名字为 的"Test1"

谢谢

4

4 回答 4

2

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

this.testObjects = new TestObjectCollection(this);

然后在 TestObjectCollection 的构造函数中:

public TestObject ParentObject { get; set; }

public TestObjectCollection(TestObject parent)
{
    ParentObject = parent;
    ...
}
于 2015-08-13T02:39:55.383 回答
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 ...   
}
于 2015-08-13T03:13:59.170 回答
0

也许你可以这样做:

using System;
using System.Collections.Generic;

public class TestObject
{
    private TestObjectCollection _testObjects;

    public string name { get; set; }
    public TestObjectCollection parentCollection { get; set; }
    public TestObjectCollection testObjects 
    { 
        get
        {
            return _testObjects;
        }
        set 
        {
            _testObjects = value;
            _testObjects.parent = this;
        }
    }
}

public class TestObjectCollection
{
    private List<TestObject> _testObjects;

    public TestObject parent { get; set; }

    public TestObjectCollection()
    {
        _testObjects = new List<TestObject>();
    }

    public void Add(TestObject testObject)
    {
        testObject.parentCollection = this;
        _testObjects.Add(testObject);
    }

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


public class Test
{
    public static void Main()
    {
        // your code goes here
        TestObject test1 = new TestObject();

        TestObject test2 = new TestObject();

        var collection = new TestObjectCollection();
        collection.Add(test2);
        test1.testObjects = collection;

        if (test2.parentCollection.parent == test1)
        {
            Console.WriteLine("Done");
        }
        else
        {
            Console.WriteLine("Fail");
        }
    }
}

我以列表为例。

于 2015-08-13T03:01:46.217 回答
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。然后它创建一个TestObjectCollection 的实例。

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

回到 TestObject 的构造函数中,它将 TestObjects 设置到它刚刚创建的集合中,然后将对自身的引用添加到静态的 TestObjects 字典中。它使用 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;
        }
    }
于 2015-08-13T03:31:51.937 回答