2015-08-13 129 views
3

在C#中,如果我有一个拥有集合的对象,是否有可能检索到包含集合的对象?如何获取集合的父对象?

下面是一个例子:

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

TestObjectCollection收集从CollectionBase继承,是TestObjects的集合。

下面是一个示例实现:

  • TestObject与的"Test1"
  • TestObject"Test1"名称具有TestObjectCollectionTestObject"Test2"
的名称的名称创建

如果我有名为"Test2"TestObject,我可以得到TestObject"Test1"

感谢

回答

2

做到这一点的唯一方法名称是保持子对象的父级的引用。

this.testObjects = new TestObjectCollection(this); 

然后在TestObjectCollection的构造函数:

public TestObject ParentObject { get; set; } 

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

许多定义良好的多级对象(就像DataSets DataTables)正式为你做了这个,就像上面的解决方案一样。关键的一点是,它是正式定义的,而不是一种黑客,你可能会通过保留一个从未预料到的引用而以某种奇怪的方式摧毁父母对象链清理例程。例如,DataRow允许访问其DataTable。 –

+0

只是好奇:“正式定义”和“黑客”有什么区别? – Yogesh

+0

如果你必须在.Net中经历非同寻常的长度,那么这是一个破解。如果你正在引用一个已经存在的属性(比如上面的解决方案),那么它不是黑客。如果你不得不在4.0或4.5中使用反射来做某件事,那就是一种破解。 –

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"); 
     } 
    } 
} 

我使用的列表作为例子,而创建子对象,你可以这样做。

0

除非你明确的代码,亲子关系(如约杰什的答案),有没有办法找到“”父 - 很大一部分原因是可以有不止一个这样的家长:

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 ... 
} 
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的Instace。

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

回到TestObject的构造函数中,它将Set的TestObject设置为它刚刚创建的集合,然后将一个引用自身添加到静态的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; 
     } 
    } 
+0

通过允许在TestObject.TestObjects上设置一个延迟加载模式(如TestObjectCollection.Parent上的Parent属性),然后从TestObject的构造函数中移除TestObjectCollection实例化,可以更新此模型以支持序列化。 –

+0

此外,即使将TestObject对象添加到TestObject.TestObjects集合仍然可以工作。因此,_Collections基本上是一个所有TestObject的平面列表,它们的子代和父代代码都不在其中。 –