如果您不想在构造函数中传递引用,则可以使用静态字典来跟踪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;
}
}
许多定义良好的多级对象(就像DataSets DataTables)正式为你做了这个,就像上面的解决方案一样。关键的一点是,它是正式定义的,而不是一种黑客,你可能会通过保留一个从未预料到的引用而以某种奇怪的方式摧毁父母对象链清理例程。例如,DataRow允许访问其DataTable。 –
只是好奇:“正式定义”和“黑客”有什么区别? – Yogesh
如果你必须在.Net中经历非同寻常的长度,那么这是一个破解。如果你正在引用一个已经存在的属性(比如上面的解决方案),那么它不是黑客。如果你不得不在4.0或4.5中使用反射来做某件事,那就是一种破解。 –