473,503 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1336
<"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
5720
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
1002
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...
26
5335
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
7205
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
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
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
7353
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...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.