471,594 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

KeyedCollection serialization with SoapFormatter

Hi,

I'm trying to serialize a KeyedCollection with the SoapFormatter,
however I'm getting the exception:

System.Runtime.Serialization.SerializationExceptio n: Soap Serializer
does not support serializing Generic Types :
System.Collections.Generic.GenericEqualityComparer `1[System.String]

I didn't expose any generic types however, so it's internal to the Keyed
Collection. Any alternatives or workarounds to this?

my test case:

[TestFixture()]
public class SoapTests
{
[Test]
public void TestKeyedCollectionSoapSerialization()
{
TestKeyedCollection collection = new TestKeyedCollection();
collection.Add(new TestItem("Test"));
using (FileStream stream = File.OpenWrite("test.soap"))
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, collection);
}
}
}

[Serializable()]
public class TestKeyedCollection : KeyedCollection<string,TestItem>
{
protected override string GetKeyForItem(TestItem item)
{
return item.Name;
}
}

[Serializable()]
public class TestItem
{
string _name;

public TestItem(string name)
{
_name = name;
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}

Best Regards,

Wiebe Tijsma
Dec 15 '06 #1
3 3881
Hi,

The work around is to use a base class that isn't generic, but since there
isn't a non-generic KeyedCollection implementation, AFAIK, you'll have to
create your own using Hashtable.

--
Dave Sexton

"Wiebe Tijsma" <wi*********@CAPITALStijsma.comwrote in message
news:ug**************@TK2MSFTNGP03.phx.gbl...
Hi,

I'm trying to serialize a KeyedCollection with the SoapFormatter, however
I'm getting the exception:

System.Runtime.Serialization.SerializationExceptio n: Soap Serializer does
not support serializing Generic Types :
System.Collections.Generic.GenericEqualityComparer `1[System.String]

I didn't expose any generic types however, so it's internal to the Keyed
Collection. Any alternatives or workarounds to this?

my test case:

[TestFixture()]
public class SoapTests
{
[Test]
public void TestKeyedCollectionSoapSerialization()
{
TestKeyedCollection collection = new TestKeyedCollection();
collection.Add(new TestItem("Test"));
using (FileStream stream = File.OpenWrite("test.soap"))
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, collection);
}
}
}

[Serializable()]
public class TestKeyedCollection : KeyedCollection<string,TestItem>
{
protected override string GetKeyForItem(TestItem item)
{
return item.Name;
}
}

[Serializable()]
public class TestItem
{
string _name;

public TestItem(string name)
{
_name = name;
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}

Best Regards,

Wiebe Tijsma

Dec 15 '06 #2
Hi Dave,

Thanks, but I thought it wasn't a problem to inherit from a generic
class, as long as it doesn't expose any generic parameters/fields.

Best Regards,

Wiebe Tijsma

Dave Sexton wrote:
Hi,

The work around is to use a base class that isn't generic, but since there
isn't a non-generic KeyedCollection implementation, AFAIK, you'll have to
create your own using Hashtable.
Dec 15 '06 #3
Hi Wiebe,

A generic type may be serialized as long as its generic parameters are
serializable as well:

".NET Development (General) Technical Articles, Generics FAQ: Best Practices
How Do I Serialize Generic Types?"
http://msdn2.microsoft.com/en-us/lib...actices_topic7

But as the error you encountered states, the SoapFormatter doesn't support
serialization of generic types:

"Issues When Using Microsoft Visual Studio 2005
2.36 Using generic types with .NET Remoting and SOAP Serialization"
http://msdn2.microsoft.com/en-us/vstudio/aa718685.aspx

There are no workarounds offered by the article I've cited above.

--
Dave Sexton

"Wiebe Tijsma" <wi*********@CAPITALStijsma.comwrote in message
news:ec**************@TK2MSFTNGP06.phx.gbl...
Hi Dave,

Thanks, but I thought it wasn't a problem to inherit from a generic class,
as long as it doesn't expose any generic parameters/fields.

Best Regards,

Wiebe Tijsma

Dave Sexton wrote:
>Hi,

The work around is to use a base class that isn't generic, but since
there isn't a non-generic KeyedCollection implementation, AFAIK, you'll
have to create your own using Hashtable.

Dec 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Scott James | last post: by
3 posts views Thread by AA | last post: by
2 posts views Thread by Oliver Sturm | last post: by
5 posts views Thread by Arjen | last post: by
3 posts views Thread by Mike Logan | last post: by
2 posts views Thread by Luck | last post: by
1 post views Thread by Dejan | last post: by
2 posts views Thread by Peter Duniho | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

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.