PC Review


Reply
Thread Tools Rate Thread

Basic class constructor and serialization

 
 
Amadrias
Guest
Posts: n/a
 
      29th Dec 2003
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
 
Reply With Quote
 
 
 
 
Codemonkey
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Jan Tielens
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> schreef in bericht
news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Amadrias
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
>news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
>> 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

>
>
>.
>

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Codemonkey
Guest
Posts: n/a
 
      29th Dec 2003
> 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" <(E-Mail Removed)> wrote in message
news:05f401c3ce13$872d9dc0$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> >news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
Amadrias
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
>news:05f401c3ce13$872d9dc0$(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> >news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
>> >> 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
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
Codemonkey
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:000a01c3ce26$38d295f0$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> >news:05f401c3ce13$872d9dc0$(E-Mail Removed)...
> >> 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" <(E-Mail Removed)> wrote in message
> >> >news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> >> >> 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
> >> >
> >> >
> >> >.
> >> >

> >
> >
> >.
> >



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      29th Dec 2003
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" <(E-Mail Removed)> wrote in message
news:05f401c3ce13$872d9dc0$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> >news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
Amadrias
Guest
Posts: n/a
 
      30th Dec 2003
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" <(E-Mail Removed)> wrote in message
>news:05f401c3ce13$872d9dc0$(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> >news:032f01c3ce0d$5e56f640$(E-Mail Removed)...
>> >> 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
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialization and parameterized constructor Stan Microsoft Dot NET Framework 1 20th Jul 2006 09:52 AM
Serialization of the class with parameterized constructor Stan Microsoft Dot NET Framework 0 17th Jul 2006 02:13 PM
HttpWebRequest and obsoleted serialization constructor Ivan Peev Microsoft Dot NET Framework 1 3rd Dec 2005 08:51 AM
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:13 AM.