Hi all,
I recently came across something really strange and after a couple of days
of debugging, I finally nailed the cause of it. However, I have absolutely no
idea what I am doing wrong or is it just a bug in binary serialization. The
following is a simple example of the code:
using System;
using System.Collecti ons.Generic;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
namespace ConsoleApplicat ion5
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B(a);
List<CcList = new List<C>();
for (int i = 0; i < 10000; i++)
{
cList.Add(new C("someValue")) ;
}
b.CList = cList;
MemoryStream stream = new MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Se rialize(stream, b);
}
}
[Serializable]
class A
{
private Dictionary<stri ng, string_dic1 = new Dictionary<stri ng,
string>();
public A()
{
_dic1.Add("key1 ", "value1");
_dic1.Add("key2 ", "value2");
}
}
[Serializable]
class B
{
private List<C_cList = new List<C>();
private A _a;
public B(A a)
{
_a = a;
}
public List<CCList
{
get { return _cList; }
set { _cList = value; }
}
}
[Serializable]
class C
{
private Dictionary<stri ng, string_dic2 = new Dictionary<stri ng,
string>();
private string _value;
public C(string value)
{
_value = value;
}
}
}
If you run the code, you will find that the stream has a length of 4,532,517
bytes. Now, try changing _dic1(Class A) to be a Dictionary<stri ng, object>
and run the code again. Now, the stream length is 462,924 bytes. Why is there
such a big difference just by changing the type? What I noticed also was that
this might be due to the fact that I have another dictionary of the same type
in Class C.
Am I doing something wrong here? If not, is this a bug?
Thanks in advance!!
I recently came across something really strange and after a couple of days
of debugging, I finally nailed the cause of it. However, I have absolutely no
idea what I am doing wrong or is it just a bug in binary serialization. The
following is a simple example of the code:
using System;
using System.Collecti ons.Generic;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
namespace ConsoleApplicat ion5
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B(a);
List<CcList = new List<C>();
for (int i = 0; i < 10000; i++)
{
cList.Add(new C("someValue")) ;
}
b.CList = cList;
MemoryStream stream = new MemoryStream();
BinaryFormatter objFormatter = new BinaryFormatter ();
objFormatter.Se rialize(stream, b);
}
}
[Serializable]
class A
{
private Dictionary<stri ng, string_dic1 = new Dictionary<stri ng,
string>();
public A()
{
_dic1.Add("key1 ", "value1");
_dic1.Add("key2 ", "value2");
}
}
[Serializable]
class B
{
private List<C_cList = new List<C>();
private A _a;
public B(A a)
{
_a = a;
}
public List<CCList
{
get { return _cList; }
set { _cList = value; }
}
}
[Serializable]
class C
{
private Dictionary<stri ng, string_dic2 = new Dictionary<stri ng,
string>();
private string _value;
public C(string value)
{
_value = value;
}
}
}
If you run the code, you will find that the stream has a length of 4,532,517
bytes. Now, try changing _dic1(Class A) to be a Dictionary<stri ng, object>
and run the code again. Now, the stream length is 462,924 bytes. Why is there
such a big difference just by changing the type? What I noticed also was that
this might be due to the fact that I have another dictionary of the same type
in Class C.
Am I doing something wrong here? If not, is this a bug?
Thanks in advance!!
Comment