473,835 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2205
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******@wanad oo.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******@wanad oo.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******@wanad oo.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******@wanad oo.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.S erializationInf o, ByVal context As
System.Runtime. Serialization.S treamingContext )
End Sub

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

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

Hope this helps,

Trev.

"Amadrias" <am******@wanad oo.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******@wanad oo.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 SerializationIn fo
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. SerializationIn fo, ByVal context AsSystem.Runtime .Serialization. StreamingContex t)
End Sub

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

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

Trev.

"Amadrias" <am******@wanad oo.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******@wanad oo.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 SerializationEx ception.
"Amadrias" <am******@wanad oo.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 SerializationIn fo
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. SerializationIn fo, ByVal

context As
System.Runtime .Serialization. StreamingContex t)
End Sub

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

In this constructor, use the "info" parameter to

deserialize your class.

Hope this helps,

Trev.

"Amadrias" <am******@wanad oo.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******@wanad oo.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******@wanad oo.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******@wanad oo.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******@wanad oo.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******@wanad oo.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

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

Similar topics

1
1499
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 a class that inherits from dataset, and I want to serialize it , so I created a serialization constructor that forwards the call to the base class (the dataset) serialization constructor, normally, for this action to succeed I am supposed to put...
0
3032
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 to an XML schema (xsd).
5
7291
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 properties.xsd this creates properties.cs
2
2359
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 a class that inherits from dataset, and I want to serialize it , so I created a serialization constructor that forwards the call to the base class (the dataset) serialization constructor, normally, for this action to succeed I am supposed to put...
7
3552
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 TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same error. The class is below ... as you can see it contains a Collection, two
11
306
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 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
2
1940
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 = System.Web.HttpContext.Current.Session.Item("mySessionVar") But I can't figure out if it is possible to access a page level variable from within a class.
3
2051
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 object into a more strongly typed class which knows what properties to expect. The basic top level class is as follows: using System;
3
6548
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 : There was an error reflecting the type XXXX Is there any way to serialize this object w/o moving away from Interface based classes. Many Thanks! Eg. //=========Interfaces===============
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10812
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10561
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9346
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7766
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4434
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 we have to send another system
2
3995
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.