Lets say I have a class:
class MyClass
{
object AnObject;
public object GetObject(BlackBoxObjectMaker ObjMaker)
{
if(AnObject == null)
AnObject = ObjMaker.MakeObject(); //MakeObject may call GetObject on this MyClass instance
return AnObject;
}
}
Whose job it is to create and provide access to objects using BlackBoxObjectMaker 'on demand' via GetObject().
Lets also say that, MyClass may be called upon to provide an object, 'x', the members of which contain members that reference 'x'.
In this case, GetObject() would end up calling itself on the same instance of MyClass, causing a stack overflow.
MyClass could be modified to detect when GetObject() is calling itself on the same instance, but in that case, how can GetObject() return a reference to AnObject that BlackBoxObjectMaker has not finished yet?
How can I provide a reference to an object, before I have a reference to that object? Is there such thing as a 'placeholder' in C#?
Extra information:
This is part of serialisation tool which is intended to support circular references. In this particular case the tool may be re-written so that a reference to an object may be taken before its properties are populated (and the reference is needed), as Henk Holterman says; but I would like an answer to this question, say for the time when BlackBoxObjectMaker really is is a black box.
This would be trivial in C++, so I am curious as to what options are available in C#.