471,602 Members | 1,308 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Deserialization bug, workaround or fix desperatly needed

TEK
There seems to be a bug when deserialization some classes in the .NET
framework.

If you try to deserialize a class that has a base class that holds a struct
with a member that is implementing the ICollection interface, this causes
the deserialization to fail.
In my case this is a huge problem, and any suggestion for a workaround would
be happily received.

A small example of a set of classes that reproduces the problem is included
at the end of the post.
I initially tought that the source of the problem was connected to my
collection having a intenal hashtable, however the same issue occure even if
none of the ICollection methods is actually implemented.

Code to test the issue:

FooA fooA = new FooA();
SerializationTest(fooA);
private void SerializationTest(object o){
FooA o = new FooA;
BinaryFormatter formatter = new BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
try{
formatter.Serialize(memStream, o);
memStream.Seek(0, System.IO.SeekOrigin.Begin);
object clone = formatter.Deserialize(memStream);
}finally{
memStream.Close();
}
}


using System;
using System.Collections;

namespace TestDeserializationIssue01
{
[Serializable]
public class FooA : FooAbstract {
public FooA(){}
}

[Serializable]
public abstract class FooAbstract
{
[Serializable]
private struct FooAbstractData{
public MyCollection myCollection;
public FooAbstractData(MyCollection collection){
myCollection = collection;
}
}
private FooAbstractData _data = new FooAbstractData(new MyCollection());

public FooAbstract(){}
}

[Serializable]
public class MyCollection : ICollection {
//Hashtable _hash;
public MyCollection(){
/*_hash = new Hashtable();*/
}
public bool IsSynchronized {
get {return false;/*return _hash.IsSynchronized;*/}
}
public int Count {
get {return 0;/*return _hash.Count;*/}
}
public void CopyTo(Array array, int index) {
/*_hash.Values.CopyTo(array, index);*/
}

public object SyncRoot {
get {return typeof(MyCollection);/*return _hash.SyncRoot;*/}
}

public IEnumerator GetEnumerator() {
return null; /*return _hash.Values.GetEnumerator();*/
}
}
}
Jul 21 '05 #1
3 1538
TEK
Cleaner classes to reproduce the issue included below.
Serializing/deserialzing class FooAbstract works with no problem.
Serializing/deserializing class FooA does not work.

[Serializable]
public class FooA : FooAbstract {
public FooA(){}
}
[Serializable]
public class FooAbstract
{
[Serializable]
private struct FooAbstractData{
public ArrayList collection;
public FooAbstractData(ArrayList col){
collection = col;
}
}
private FooAbstractData _data;

public FooAbstract(){
_data = new FooAbstractData(new ArrayList());
}
}
Jul 21 '05 #2
TEK
Even more info.
The issue is actually not related to ICollection or any list at all.
If there is any class value included in the struct, the serialization will
fail for the inherited class.
If the value is a "native" type as string, int, Guid or a new struct, it
will work.

TEK
Jul 21 '05 #3
Try with XmlInclude(System.Collection......).
I have had recently a problem when serializing enums, i solved it by
changing the place where i declare the enum. Originally it was declared in
the class that used it. But the XmlSerializer did not work well with its
values, so I changed this configuration and put the enum in the top class of
the object model's

I apologise by my english
"TEK" <trond-eirik.at.kolloen.no> escribió en el mensaje
news:e7**************@tk2msftngp13.phx.gbl...
Even more info.
The issue is actually not related to ICollection or any list at all.
If there is any class value included in the struct, the serialization will
fail for the inherited class.
If the value is a "native" type as string, int, Guid or a new struct, it
will work.

TEK

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by dave v | last post: by
1 post views Thread by Tom L | last post: by
3 posts views Thread by parrot toes | last post: by
5 posts views Thread by Greg Allen | last post: by
reply views Thread by =?Utf-8?B?UGllcmNlQnJvc25hbg==?= | last post: by
6 posts views Thread by Joe | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | 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.