473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

instances and constructors: creating using System.Type

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
Nov 16 '05 #1
4 950
<"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
Nov 16 '05 #2
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

Nov 16 '05 #3
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

Nov 16 '05 #4
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
4
by: Jamie B | last post by:
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...
4
by: Jamie B | last post by:
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...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.