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