.Net二进制序列化中的一个错误? [英] A bug in .Net Binary Serialization?
问题描述
大家好,
我最近遇到了一些非常奇怪的事情,经过几天的调试后,我终于找到了原因。但是,我绝对不知道我做错了什么,或者它只是二进制序列化中的一个错误。
以下是代码的一个简单示例:
使用System;
使用System.Collections。 Generic;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
命名空间ConsoleApplication5
{
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.Serialize(stream,b);
}
}
[可序列化]
A类
{
private Dictionary< string,string_dic1 = new Dictionary< string,
st ring>();
public A()
{
_dic1.Add(" key1"," value1" ;);
_dic1.Add(" key2"," value2");
}
}
[Serializable]
class B
{
private List< C_cList = new List< C>();
私人A _a;
公共B(A a)
{
_a = a;
}
公开列表< CCList
{
get {return _cList; }
set {_cList = value; }
}
}
[可序列化]
class C
{
private Dictionary< string,string_dic2 = new Dictionary< string,
string>();
private string _value;
公共C(字符串值)
{
_value = value;
}
}
}
/>
如果运行代码,您会发现该流的长度为4,532,517
字节。现在,尝试将_dic1(A类)更改为Dictionary< string,object>
并再次运行代码。现在,流长度为462,924字节。为什么仅仅通过更改类型就可以获得如此巨大的差异?我注意到的还有
这可能是因为我在C组中有另一本相同类型的字典
我在这里做错了吗?如果没有,这是一个错误吗?
提前致谢!!
2008年7月1日星期二22:50:00 -0700,ztRon
< zt *** @ discussion.microsoft.comwrote:
[...]
如果您运行代码,您会发现该流的长度为
4,532,517
字节。现在,尝试将_dic1(A类)更改为Dictionary< string,
对象>
并再次运行代码。现在,流长度为462,924字节。为什么
那里
只是通过更改类型这么大的差异?我注意到的还有
这可能是因为我有另一本相同的词典
类型
C级
我在这里做错了吗?如果没有,这是一个错误吗?
我会投票错误。但我承认,我不是序列化专家,所以我可能会错过一些东西。
但我同意这么简单似乎很了不起更改字典< TKey的
类型参数,TValueinstance会产生这样的显着差异。事实上我确认了这种行为(尽管
的数字与输出略有不同......但相对比例是
相同)。
尝试序列化为更易读的格式会很有趣。
看看具体的差异是什么。我没有时间在
的时刻去探索太多,但这可能是你想尝试的。
Pete
对不起,我的帖子之间似乎有一个巨大的空白区域。转发下面:
大家好,
的调试,我终于找到了它的原因。但是,我绝对不知道我做错了什么,或者它只是二进制序列化中的一个错误。
以下是代码的简单示例:
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Runtime.Serialization.Formatters.Binary;
命名空间ConsoleApplication5
{
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.Serialize(stream,b);
}
}
[Serializable]
class A
{
private Dictionary< string,string_dic1 = new Dictionary< string,
string> ;();
public A()
{
_dic1.Add(" key1"," value1" );
_dic1.Add(" key2"," value2");
}
}
>
[Serializable]
class B
{
private List< C_cList = new List< C>();
私人A _a;
公共B(A a)
{
_a = a; < br $>
}
公开列表< CCList
{
get {return _cList; }
set {_cList = value; }
}
}
[可序列化]
class C
{
private Dictionary< string,string_dic2 = new Dictionary< string,
string>();
private string _value;
公共C(字符串值)
{
_value = value;
}
}
}
如果您运行代码,您会发现该流的长度为4,532,517
个字节。现在,尝试将_dic1(A类)更改为Dictionary< string,object>
并再次运行代码。现在,流长度为462,924字节。为什么仅仅通过更改类型就可以获得如此巨大的差异?我注意到的还有
这可能是因为我在C组中有另一本相同类型的字典
我在这里做错了吗?如果没有,这是一个错误吗?
提前致谢!!
尝试序列化为一个很有趣更易读的格式和
看看具体的差异是什么。我没有时间在
的时刻去探索太多,但这可能是你想尝试的。
这个例子实际上来自一个更复杂的代码,如果这是你的意思
。在我的单元测试中,我注意到最近的尺寸因为添加了一个字典而增加了三倍,即使它只有一个实例。这是我开始调试的时候,最终能够确定其原因,并提出了一个更简单的例子来表达这个
问题。
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.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication5
{
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.Serialize(stream, b);
}
}
[Serializable]
class A
{
private Dictionary<string, string_dic1 = new Dictionary<string,
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<string, string_dic2 = new Dictionary<string,
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<string, 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!!
On Tue, 01 Jul 2008 22:50:00 -0700, ztRon
<zt***@discussions.microsoft.comwrote:
[...]
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<string,
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?I''ll vote bug. But I admit, I''m no serialization expert so I might be
missing something.
But I do agree that it seems remarkable that such a simple change in the
type parameter for a Dictionary<TKey, TValueinstance would produce such
a dramatic difference. And I have in fact confirmed the behavior (albeit
with slightly different numbers as the output...but the relative scale is
the same).
It would be interesting to try to serialize to a more readable format and
see what the specific differences are. I don''t have the time at the
moment to explore too much, but it''s something you might like to try.
Pete
Sorry my post seems to have a huge white space in between. Reposting it below:
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.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication5
{
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.Serialize(stream, b);
}
}
[Serializable]
class A
{
private Dictionary<string, string_dic1 = new Dictionary<string,
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<string, string_dic2 = new Dictionary<string,
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<string, 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!!
It would be interesting to try to serialize to a more readable format andsee what the specific differences are. I don''t have the time at the
moment to explore too much, but it''s something you might like to try.This example was actually derived from a more complex code if that was what
you meant. And in my unit testing of it, I noticed that the size recently
tripled due to the addition of one dictionary even though there is only ever
one instance of it. This was when I started to debug and finally were able to
pinpoint its cause and came out with a simpler example to express this
problem.
这篇关于.Net二进制序列化中的一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!