By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,728 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,728 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

System.Runtime.Serialization.SerializationExceptio n was unhandled
Message="The constructor to deserialize an object of type
'WindowsApplication10.Form1+DictionaryExt`2[System.String,WindowsApplication10.Form1+WhateverC lass]'
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, WhateverClassMyDictionaryExt = new
DictionaryExt<string,
WhateverClass>(StringComparer.InvariantCultureIgno reCase);
//Serialization
DictionaryExt<string, WhateverClassClone =
(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.Bin aryFormatter().Serialize(MemoryStream,
MyObject);
MemoryStream.Position = 0;
return new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter().Deserialize(MemoryStream);
}
}
}

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

public DictionaryExt(IEqualityComparer<KeyIEqualityCompar er)
: base(IEqualityComparer)
{
}
}

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

}
}

Feb 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
if you want a serializable (xml) dictionary why not try

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

HTH

Ollie Riches

"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>
Hi, please find below a very simple code snippet which is giving me
the following error:

System.Runtime.Serialization.SerializationExceptio n was unhandled
Message="The constructor to deserialize an object of type
'WindowsApplication10.Form1+DictionaryExt`2[System.String,WindowsApplication10.Form1+WhateverC lass]'
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, WhateverClassMyDictionaryExt = new
DictionaryExt<string,
WhateverClass>(StringComparer.InvariantCultureIgno reCase);
//Serialization
DictionaryExt<string, WhateverClassClone =
(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.Bin aryFormatter().Serialize(MemoryStream,
MyObject);
MemoryStream.Position = 0;
return new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter().Deserialize(MemoryStream);
}
}
}

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

public DictionaryExt(IEqualityComparer<KeyIEqualityCompar er)
: base(IEqualityComparer)
{
}
}

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

}
}

Feb 12 '07 #2

P: n/a
On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
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

Feb 12 '07 #3

P: n/a
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" <pa***********@libero.itwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
>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

Feb 12 '07 #4

P: n/a
On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
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

Feb 12 '07 #5

P: n/a
On 12 Feb 2007 07:59:03 -0800, "pamela fluente"
<pa***********@libero.itwrote:
>On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
>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
Feb 13 '07 #6

P: n/a
Phil, you only need a parameterless constructor for xml serialisation.

Ollie Riches
<Ph***********@foo.comwrote in message
news:1f********************************@4ax.com...
On 12 Feb 2007 07:59:03 -0800, "pamela fluente"
<pa***********@libero.itwrote:
>>On 12 Feb, 15:57, "Ollie Riches" <ollie_ric...@hotmail.comwrote:
>>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

Feb 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.