instances and constructors: creating using System.Type

J

Jamie B

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. The only caveat is that before I call the
constructor I still want the object to correctly return its type if
GetType() is called.

If its possible, how do I create the initial object, and how do I then later
call the constructor on it?

Thanks

Jamie Briant
 
J

Jon Skeet [C# MVP]

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?
 
J

Jamie B

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");

}

}
 
J

Jamie B

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 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");

}

}
 
J

Jamie B

FormatterServices.GetUnitializedObject
"Jamie B @species.org>" <jab<dontspamme> wrote in message 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 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");

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top