473,382 Members | 1,705 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,382 software developers and data experts.

Basic class constructor and serialization

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
Jul 21 '05 #1
11 2145
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" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...
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

Jul 21 '05 #2
You could write your own serializer and de-serializer, but this would be
quite some work...

--
Greetz,
Jan
________________________
Read my weblog: http://weblogs.asp.net/jan

"Amadrias" <am******@wanadoo.fr> schreef in bericht
news:03****************************@phx.gbl...
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

Jul 21 '05 #3
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 emptyconstructor as private or protected.

Hope this helps,

Trev.
"Amadrias" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...
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

.

Jul 21 '05 #4
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" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl... 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

Jul 21 '05 #5
> 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" <am******@wanadoo.fr> wrote in message
news:05****************************@phx.gbl... 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" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...
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

.

Jul 21 '05 #6
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 thismyself in the past that required me to have the empty constructor. The helpseemed to suggest that the framework used reflection to call theconstructor - 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 themeantime, let me know.

As Jay mentioned, ISerializable requires the following constructor:
--------------

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

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

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

Trev.

"Amadrias" <am******@wanadoo.fr> wrote in message
news:05****************************@phx.gbl...
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" <am******@wanadoo.fr> wrote in message
>news:03****************************@phx.gbl...
>> 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
>
>
>.
>

.

Jul 21 '05 #7
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" <am******@wanadoo.fr> wrote in message
news:00****************************@phx.gbl...
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" <am******@wanadoo.fr> wrote in message
news:05****************************@phx.gbl...
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" <am******@wanadoo.fr> wrote in message
>news:03****************************@phx.gbl...
>> 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
>
>
>.
>

.

Jul 21 '05 #8
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" <am******@wanadoo.fr> wrote in message
news:05****************************@phx.gbl...
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" <am******@wanadoo.fr> wrote in message
news:03****************************@phx.gbl...
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

.

Jul 21 '05 #9
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 & protectedconstructors via a special option on the "reflection" call that it is usingto 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" <am******@wanadoo.fr> wrote in message
news:05****************************@phx.gbl...
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" <am******@wanadoo.fr> wrote in message
>news:03****************************@phx.gbl...
>> 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
>
>
>.
>

.

Jul 21 '05 #10
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" <am******@wanadoo.fr> wrote in message
news:08****************************@phx.gbl... Thanks jay,

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

Amadrias

<<snip>>
Jul 21 '05 #11
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
-----Original Message-----
Amadrias,
- XML for clear object transportation over the networkAre you using the SOAP Formatter (SoapFormatter) or the

Xml Serializer(XmlSerializer) directly? Or are you staying above that level and lettingthe Framework do the XMl serialization (via a WebMethod for example)?
- Binary for file serialization.Binary serialization fully supports private default

constructors and privateISerializable constructors. I use private and protected ISerializableconstructors in one of my projects!

Can you post a 'complete' small example of your code that is failing?
Hope this helps
Jay

"Amadrias" <am******@wanadoo.fr> wrote in message
news:08****************************@phx.gbl...
Thanks jay,

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

Amadrias

<<snip>>
.

Jul 21 '05 #12

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

Similar topics

1
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
2
by: Corey B | last post by:
Is there a way for an instance of a custom class to access an ASPX page level variable? I know that I can access a Session variable from within a class using the following code: myClassVar =...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
3
by: Tantr Mantr | last post by:
Hello , I have a class which I serialize using XMLSerializer. This class has public properties which are based on other interfaces. Because of this I am unable to serialize the object. Error :...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.