If you don't want to pass referrences in the constructors, you can use a static dictionary to keep track of TestObject instances and have the TestObjectCollection look up it's parent from that static dictionary in a lazy loading fashion.
For example
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();
}
}
Testing...
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);
}
}
So what this does is in the constructor of TestObject it give's itself a GUID ID. Then it creates an Instace of TestObjectCollection.
In the Constructor of TestObjectCollection it give's itself a GUID ID.
Back in the constructor of TestObject it Set's TestObjects to the collection it just created, then adds a referrence to itself to the Dictionary of TestObjects which is static. It uses TestObject's ID as the Key for said Dictionary.
Then in TestObjectCollection it get's the parent collection by looking it up in that Static Dictionary using a property that doesn't set itself till it's called (as you can't determine it in the constructor because the TestObject constructor hasn't added the referrence yet).
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;
}
}