FormatterServices.GetUnitializedObject
"Jamie B @species.org>" <jab<dontspamme> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Ok, I have discovered that I can invoke the ConstructorInfo just like a normal method, i.e. on an already constructed object. However, what I can't seem to do is create an "empty" object without calling one of the defined constructors. That is, if I put a break point in CircSerial(), then it gets hit no matter what I do. However, the break point is not triggered when using the binary formatter. That is, the binary formatter creates a valid object without calling any of my constructors. I'm sure it even says somewhere that it does this, but I can't find anything more.
Jamie
"Jamie B @species.org>" <jab<dontspamme> wrote in message news:Ol**************@tk2msftngp13.phx.gbl...
I'm deserializing objects using a custom IFormatter. For reasons of circularity, I want to be able to create a valid object pointer without calling the Constructor( SerializationInfo info, StreamingContext context ) required by ISerializable. Then, at a later time, go back and call the above constructor.
I ask, because BinaryFormatter is definitely doing something like this. If two objects point to each other, neither one can exist until their constructors return. And yet, in the example below, the constructor of the first is called with a valid reference to the second. The second hasnt yet had its CircSerial( SerializationInfo info, StreamingContext context ) called. But it does later.
How to do it?
Thank,
Jamie
[Serializable()]
public class CircSerial : ISerializable
{
public CircSerial()
{
other = null;
}
public CircSerial( SerializationInfo info, StreamingContext stream )
{
other = (CircSerial) info.GetValue( "other", typeof( CircSerial ));
}
public CircSerial other;
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue( "other", other );
}
static public void Test()
{
CircSerial first = new CircSerial();
CircSerial second = new CircSerial();
first.other = second;
second.other = first;
SerialHelpers.Serialize( first, @".\circserial.tst" );
object r = SerialHelpers.Deserialize( @".\circserial.tst" );
Console.WriteLine("Done");
}
}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
<"Jamie B" <jab<dontspamme>@species.org>> wrote: Is it possible to use System.Type, Activator or whatever to create a
complete "blank", unitialized object of a given type, and then at a later
time call a constructor on it.
No. You can't create an object without calling a constructor. Why not
just keep the Type reference instead?
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too