Deserialization bug, workaround or fix desperatly needed

  • Thread starter Thread starter TEK
  • Start date Start date
T

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();*/
}
}
}
 
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());
}
}
 
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
 
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
 
Back
Top