PC Review


Reply
Thread Tools Rate Thread

Derived Dictionary serialization: please help me find the error in this simple code snippet

 
 
pamela fluente
Guest
Posts: n/a
 
      12th Feb 2007

Hi, please find below a very simple code snippet which is giving me
the following error:

System.Runtime.Serialization.SerializationException was unhandled
Message="The constructor to deserialize an object of type
'WindowsApplication10.Form1+DictionaryExt`2[System.String,WindowsApplication10.Form1+WhateverClass]'
was not found."
Source="mscorlib"

You can just past the snippet on any form with a button.

Could anyone please point out how I can correct the code below.
I must be missing something simple, but I can't see what.
How do I specify the "deserialization constructor"?

Thanks.


PS
Also what is " DictionaryExt`2 ", there is no symbol with such a name
in the snippet !?



------------------------------ CODE
--------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
DictionaryExt<string, WhateverClass> MyDictionaryExt = new
DictionaryExt<string,
WhateverClass>(StringComparer.InvariantCultureIgnoreCase);
//Serialization
DictionaryExt<string, WhateverClass> Clone =
(DictionaryExt<string,
WhateverClass>)CloneObjectInMemory(MyDictionaryExt);
}


public object CloneObjectInMemory(object MyObject)
{
using (System.IO.MemoryStream MemoryStream = new
System.IO.MemoryStream()) {
{
new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(MemoryStream,
MyObject);
MemoryStream.Position = 0;
return new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(MemoryStream);
}
}
}

[Serializable()]
public class DictionaryExt<Key, value> :
System.Collections.Generic.Dictionary<Key, value>
{

public DictionaryExt(IEqualityComparer<Key> IEqualityComparer)
: base(IEqualityComparer)
{
}
}

public class WhateverClass
{
public string Greeting = "Hi";
public int Age;
}

}
}

 
Reply With Quote
 
 
 
 
Ollie Riches
Guest
Posts: n/a
 
      12th Feb 2007
if you want a serializable (xml) dictionary why not try

http://weblogs.asp.net/pwelter34/arc...03/444961.aspx

HTH

Ollie Riches

"pamela fluente" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Hi, please find below a very simple code snippet which is giving me
> the following error:
>
> System.Runtime.Serialization.SerializationException was unhandled
> Message="The constructor to deserialize an object of type
> 'WindowsApplication10.Form1+DictionaryExt`2[System.String,WindowsApplication10.Form1+WhateverClass]'
> was not found."
> Source="mscorlib"
>
> You can just past the snippet on any form with a button.
>
> Could anyone please point out how I can correct the code below.
> I must be missing something simple, but I can't see what.
> How do I specify the "deserialization constructor"?
>
> Thanks.
>
>
> PS
> Also what is " DictionaryExt`2 ", there is no symbol with such a name
> in the snippet !?
>
>
>
> ------------------------------ CODE
> --------------------------------------------------
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
>
> namespace WindowsApplication10
> {
> public partial class Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
> }
>
> private void button1_Click(object sender, EventArgs e)
> {
> DictionaryExt<string, WhateverClass> MyDictionaryExt = new
> DictionaryExt<string,
> WhateverClass>(StringComparer.InvariantCultureIgnoreCase);
> //Serialization
> DictionaryExt<string, WhateverClass> Clone =
> (DictionaryExt<string,
> WhateverClass>)CloneObjectInMemory(MyDictionaryExt);
> }
>
>
> public object CloneObjectInMemory(object MyObject)
> {
> using (System.IO.MemoryStream MemoryStream = new
> System.IO.MemoryStream()) {
> {
> new
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(MemoryStream,
> MyObject);
> MemoryStream.Position = 0;
> return new
> System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(MemoryStream);
> }
> }
> }
>
> [Serializable()]
> public class DictionaryExt<Key, value> :
> System.Collections.Generic.Dictionary<Key, value>
> {
>
> public DictionaryExt(IEqualityComparer<Key> IEqualityComparer)
> : base(IEqualityComparer)
> {
> }
> }
>
> public class WhateverClass
> {
> public string Greeting = "Hi";
> public int Age;
> }
>
> }
> }
>



 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      12th Feb 2007
On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
> if you want a serializable (xml) dictionary why not try
>
> http://weblogs.asp.net/pwelter34/arc...03/444961.aspx
>


Thanks Ollie ,

as you can see I am doing *binary* serialization, not XML.

In any case, I need to understand well the problem here, because
actually this is
just an example of several colllections I am deriving and that have
the same issue.

So what's the real problem here and what's the code correction ?

Any help ?


-Pam

 
Reply With Quote
 
Ollie Riches
Guest
Posts: n/a
 
      12th Feb 2007
you aree going to require a binary deserialization constructor as well as an
implementation of the interface method GetObjectData() on your dictionary
class.

a vb.net example

http://www.knowdotnet.com/articles/e...alization.html

HTH

Ollie Riches



"pamela fluente" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
>> if you want a serializable (xml) dictionary why not try
>>
>> http://weblogs.asp.net/pwelter34/arc...03/444961.aspx
>>

>
> Thanks Ollie ,
>
> as you can see I am doing *binary* serialization, not XML.
>
> In any case, I need to understand well the problem here, because
> actually this is
> just an example of several colllections I am deriving and that have
> the same issue.
>
> So what's the real problem here and what's the code correction ?
>
> Any help ?
>
>
> -Pam
>



 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      12th Feb 2007
On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
> if you want a serializable (xml) dictionary why not try
>
> http://weblogs.asp.net/pwelter34/arc...03/444961.aspx
>
> HTH
>
> Ollie Riches


Thanks Ollie ,

I have been serializing several objects and I know serialization
basics.

Before I used collections such us hashtables, or Sorted List and I
never had any problem.

Here I am not clear what is the problem Why a typed collection needs a
constructor
while a not typed one doesn't ? It would seem to me that a typed
collection should store more information.

So actually I am lost here, and the examples I have seen so far do not
spread any light ! :-(

Does anyone have an example to understand what is going on here ?
Can anyone show the actual code corrections to my simple snippet ?

-Pam

 
Reply With Quote
 
PhilipDaniels@foo.com
Guest
Posts: n/a
 
      13th Feb 2007
On 12 Feb 2007 07:59:03 -0800, "pamela fluente"
<(E-Mail Removed)> wrote:

>On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
>> if you want a serializable (xml) dictionary why not try
>>
>> http://weblogs.asp.net/pwelter34/arc...03/444961.aspx
>>
>> HTH
>>
>> Ollie Riches

>
>Thanks Ollie ,
>
>I have been serializing several objects and I know serialization
>basics.
>
>Before I used collections such us hashtables, or Sorted List and I
>never had any problem.
>
>Here I am not clear what is the problem Why a typed collection needs a
>constructor
>while a not typed one doesn't ? It would seem to me that a typed
>collection should store more information.
>
>So actually I am lost here, and the examples I have seen so far do not
>spread any light ! :-(
>
>Does anyone have an example to understand what is going on here ?
>Can anyone show the actual code corrections to my simple snippet ?
>
>-Pam


Couple of thoughts:

Doesn't deserialization always require a parameterless constructor?
Your class doesn't have one. However, adding one doesn't fix the
problem I am afraid!

I suspect that somehow you need to tell deserialize the type
parameters used to instantiate the generic. There must be some code
out on the net that does that. This might be a good starting point


http://groups.google.com/group/micro...3ca1777cf82674



--
Philip Daniels
 
Reply With Quote
 
Ollie Riches
Guest
Posts: n/a
 
      13th Feb 2007
Phil, you only need a parameterless constructor for xml serialisation.

Ollie Riches


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 12 Feb 2007 07:59:03 -0800, "pamela fluente"
> <(E-Mail Removed)> wrote:
>
>>On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.com> wrote:
>>> if you want a serializable (xml) dictionary why not try
>>>
>>> http://weblogs.asp.net/pwelter34/arc...03/444961.aspx
>>>
>>> HTH
>>>
>>> Ollie Riches

>>
>>Thanks Ollie ,
>>
>>I have been serializing several objects and I know serialization
>>basics.
>>
>>Before I used collections such us hashtables, or Sorted List and I
>>never had any problem.
>>
>>Here I am not clear what is the problem Why a typed collection needs a
>>constructor
>>while a not typed one doesn't ? It would seem to me that a typed
>>collection should store more information.
>>
>>So actually I am lost here, and the examples I have seen so far do not
>>spread any light ! :-(
>>
>>Does anyone have an example to understand what is going on here ?
>>Can anyone show the actual code corrections to my simple snippet ?
>>
>>-Pam

>
> Couple of thoughts:
>
> Doesn't deserialization always require a parameterless constructor?
> Your class doesn't have one. However, adding one doesn't fix the
> problem I am afraid!
>
> I suspect that somehow you need to tell deserialize the type
> parameters used to instantiate the generic. There must be some code
> out on the net that does that. This might be a good starting point
>
>
> http://groups.google.com/group/micro...3ca1777cf82674
>
>
>
> --
> Philip Daniels



 
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 of derived List<T> properties swiftalpha@gmail.com Microsoft C# .NET 1 29th May 2008 11:05 AM
Serialization of the class derived from CollectionBase Just D. Microsoft C# .NET 2 28th Jan 2005 02:20 PM
Serialization and derived classes StuartM Microsoft C# .NET 3 10th Nov 2004 01:52 PM
serialization problem with a class derived from dataset ofer Microsoft Dot NET Framework 0 26th Aug 2004 07:34 AM
SoapFormatter, Serialization of a DictionaryBase derived class - Please help a Microsoft VB .NET 0 1st Mar 2004 11:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:34 PM.