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

P

pamela fluente

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;
}

}
}
 
P

pamela fluente

if you want a serializable (xml) dictionary why not try

http://weblogs.asp.net/pwelter34/archive/2006/05/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
 
P

pamela fluente

if you want a serializable (xml) dictionary why not try

http://weblogs.asp.net/pwelter34/archive/2006/05/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
 
P

PhilipDaniels

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/micr...deserialization&rnum=2&hl=en#de3ca1777cf82674
 
O

Ollie Riches

Phil, you only need a parameterless constructor for xml serialisation.

Ollie Riches
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top