如何获取集合的父对象?
How can I get a collection's parent object?
在 C# 中,如果我有一个包含集合的对象,是否可以检索包含该集合的对象?
这是一个例子:
public class TestObject
{
public string name { get; set; }
public TestObjectCollection testObjects{ get; set; }
}
TestObjectCollection
集合继承自CollectionBase
,是TestObjects
的集合。
这是一个示例实现:
- 创建了一个
TestObject
,名称为 "Test1"
- 名称为
"Test1"
的 TestObject
有一个 TestObjectCollection
TestObject
名称为 "Test2"
如果我有名字为"Test2"
的TestObject
,我怎样才能得到名字为"Test1"
的TestObject
谢谢
唯一的方法是在子对象中保留对父对象的引用。您可以在创建子对象时执行此操作:
this.testObjects = new TestObjectCollection(this);
然后在 TestObjectCollection 的构造函数中:
public TestObject ParentObject { get; set; }
public TestObjectCollection(TestObject parent)
{
ParentObject = parent;
...
}
也许你可以这样做:
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");
}
}
}
我以List为例
除非您明确地编写父子关系代码(如 Yogesh 的回答),否则无法找到 "the" 父级 - 很大程度上是因为可能有不止一个这样的父级:
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 ...
}
如果您不想在构造函数中传递引用,您可以使用静态字典来跟踪 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;
}
}
在 C# 中,如果我有一个包含集合的对象,是否可以检索包含该集合的对象?
这是一个例子:
public class TestObject
{
public string name { get; set; }
public TestObjectCollection testObjects{ get; set; }
}
TestObjectCollection
集合继承自CollectionBase
,是TestObjects
的集合。
这是一个示例实现:
- 创建了一个
TestObject
,名称为"Test1"
- 名称为
"Test1"
的TestObject
有一个TestObjectCollection
TestObject
名称为"Test2"
如果我有名字为"Test2"
的TestObject
,我怎样才能得到名字为"Test1"
TestObject
谢谢
唯一的方法是在子对象中保留对父对象的引用。您可以在创建子对象时执行此操作:
this.testObjects = new TestObjectCollection(this);
然后在 TestObjectCollection 的构造函数中:
public TestObject ParentObject { get; set; }
public TestObjectCollection(TestObject parent)
{
ParentObject = parent;
...
}
也许你可以这样做:
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");
}
}
}
我以List为例
除非您明确地编写父子关系代码(如 Yogesh 的回答),否则无法找到 "the" 父级 - 很大程度上是因为可能有不止一个这样的父级:
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 ...
}
如果您不想在构造函数中传递引用,您可以使用静态字典来跟踪 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;
}
}