Basic class constructor and serialization

  • Thread starter Thread starter Amadrias
  • Start date Start date
A

Amadrias

Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias
 
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.
 
You could write your own serializer and de-serializer, but this would be
quite some work...
 
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias
-----Original Message-----
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Amadrias said:
Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.
 
Amadrias,
As Codemonkey suggested:
private MyClass(){}

Or if this is a base class, use protected.
Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?
ISerializable requires a special parameterized constructor, that requires
logic in it to do the deserialization itself!

The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay

Amadrias said:
Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias
 
Unfortunatelly, if you declare the constructor
as private or protected, it can not be
called by the .Net framework
and then it doesn't work...

Are you sure about this? I seem to remember having a problem like this
myself in the past that required me to have the empty constructor. The help
seemed to suggest that the framework used reflection to call the
constructor - thus it didn't matter if it was private or public scope.

If I can find the article, I'll post it here. If you get it working in the
meantime, let me know.

As Jay mentioned, ISerializable requires the following constructor:

--------------

Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)


End Sub

--------------

In this constructor, use the "info" parameter to deserialize your class.

Hope this helps,

Trev.

Amadrias said:
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias
-----Original Message-----
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Amadrias said:
Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.
 
Thanks Trev.

I am not completely sure about it but I already met this
problem before. And trying to solve it, I did try both
solutions:

1. Use the private empty constructor.
Result: Runtime Exception: asking for empty constructor.

2. Implement ISerializable using SerializationInfo
Result: Runtime Exception: asking for empty constructor.

I also tried to put both at the same time and it didn't
work neither.

As I mentionned above, it has been some months since I
tried this and it worth trying it again.

I'll keep you updated about it...

Amadrias
-----Original Message-----
Unfortunatelly, if you declare the constructor
as private or protected, it can not be
called by the .Net framework
and then it doesn't work...

Are you sure about this? I seem to remember having a problem like this
myself in the past that required me to have the empty constructor. The help
seemed to suggest that the framework used reflection to call the
constructor - thus it didn't matter if it was private or public scope.

If I can find the article, I'll post it here. If you get it working in the
meantime, let me know.

As Jay mentioned, ISerializable requires the following constructor:

--------------

Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)


End Sub

--------------

In this constructor, use the "info" parameter to deserialize your class.

Hope this helps,

Trev.

Amadrias said:
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias
-----Original Message-----
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute
to
the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.


.
 
Sorry it hasn't worked. If you could possibly post a short example of how to
recreate the problem (maybe a simple small class), you might be able to get
better help. A full description of the exception and stack trace might help
too. What type of exception is being thorwn? The one I've seen before is
usually a SerializationException.


Amadrias said:
Thanks Trev.

I am not completely sure about it but I already met this
problem before. And trying to solve it, I did try both
solutions:

1. Use the private empty constructor.
Result: Runtime Exception: asking for empty constructor.

2. Implement ISerializable using SerializationInfo
Result: Runtime Exception: asking for empty constructor.

I also tried to put both at the same time and it didn't
work neither.

As I mentionned above, it has been some months since I
tried this and it worth trying it again.

I'll keep you updated about it...

Amadrias
-----Original Message-----
Unfortunatelly, if you declare the constructor
as private or protected, it can not be
called by the .Net framework
and then it doesn't work...

Are you sure about this? I seem to remember having a problem like this
myself in the past that required me to have the empty constructor. The help
seemed to suggest that the framework used reflection to call the
constructor - thus it didn't matter if it was private or public scope.

If I can find the article, I'll post it here. If you get it working in the
meantime, let me know.

As Jay mentioned, ISerializable requires the following constructor:

--------------

Protected Sub New(ByVal info As
System.Runtime.Serialization.SerializationInfo, ByVal context As
System.Runtime.Serialization.StreamingContext)


End Sub

--------------

In this constructor, use the "info" parameter to deserialize your class.

Hope this helps,

Trev.

Amadrias said:
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias

-----Original Message-----
I'm not exactly sure, but I think it will work even if
you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to
the
class.

My problem is that this class is part of a framework
and
that I do not want developers to call empty
constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.


.
 
Amadrias,
The serialization code has the ability to call private & protected
constructors via a special option on the "reflection" call that it is using
to create the object indirectly.

See the articles in my other post for details.

Are you using Binary serialization or XML serialization?

Hope this helps
Jay

Amadrias said:
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias
-----Original Message-----
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Amadrias said:
Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute to the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.
 
Thanks jay,

I am doing both serialization:
- XML for clear object transportation over the network
- Binary for file serialization.

Amadrias
-----Original Message-----
Amadrias,
The serialization code has the ability to call private & protected
constructors via a special option on the "reflection" call that it is using
to create the object indirectly.

See the articles in my other post for details.

Are you using Binary serialization or XML serialization?

Hope this helps
Jay

Amadrias said:
Unfortunatelly, if you declare the constructor as private
or protected, it can not be called by the .Net framework
and then it doesn't work...

Still needs the empty constructor...

Thanks anyway...

Amadrias
-----Original Message-----
I'm not exactly sure, but I think it will work even if you define the empty
constructor as private or protected.

Hope this helps,

Trev.


Hi all,

I am using a class to transport some data over the
network. I then added the [Serializable] attribute
to
the
class.

My problem is that this class is part of a framework and
that I do not want developers to call empty constructors.
But the runtime sends me an exception when I try to
serialize this class asking me to provide it with an
empty constructor such as:

public MyClass(){}

Is there a way to avoid that problem knowing that using
the ISerializable interface, it stills ask me to
implement an empty constructor?

Thanks for the help.

Amadrias


.


.
 
Amadrias,
- XML for clear object transportation over the network
Are you using the SOAP Formatter (SoapFormatter) or the Xml Serializer
(XmlSerializer) directly? Or are you staying above that level and letting
the Framework do the XMl serialization (via a WebMethod for example)?
- Binary for file serialization.
Binary serialization fully supports private default constructors and private
ISerializable constructors. I use private and protected ISerializable
constructors in one of my projects!

Can you post a 'complete' small example of your code that is failing?

Hope this helps
Jay

Amadrias said:
Thanks jay,

I am doing both serialization:
- XML for clear object transportation over the network
- Binary for file serialization.

Amadrias
<<snip>>
 
Thanks Jay,

I found the answer.

It seems that the private empty constructor do not work
for a reason I can not explain as I am replicating some
examples I found on the web.

But it works perfectly with the ISerializable
implementation with the associated constructor with a
SerializationInfo and StreamingContext parameter.

I really thank you for the time you spent with me on the
problem.

Cordially,

Amadrias
 
Back
Top