为什么.net通用字典如此之大

前端之家收集整理的这篇文章主要介绍了为什么.net通用字典如此之大前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在VB.net中序列化一个通用字典,我很惊讶它是一个单项的大约1.3kb.我做错了什么,还是我应该做的其他事情?我有大量的词典,它正在杀死我把它们全部发送到网上.我用于序列化的代码
  1. Dim dictionary As New Dictionary(Of Integer,Integer)
  2. Dim stream As New MemoryStream
  3. Dim bformatter As New BinaryFormatter()
  4.  
  5. dictionary.Add(1,1)
  6.  
  7. bformatter.Serialize(stream,dictionary)
  8.  
  9. Dim len As Long = stream.Length
字典的默认序列化必须包括字典类型的类型信息,使用的比较器以及每个项目的类型(键和值),因为它们通常可能是子类型.必须为每个字典添加此开销.如果将数据打印为字符串,则可以看到有许多完全限定类型占用了大量字节:

\0\0\0\0????\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.Dictionary2[[System.Int32,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089],[System.Int32,PublicKeyToken=b77a5c561934e089]]\0\0\0\aVersion\bComparer\bHashSize\rKeyValuePairs\0\0\b?System.Collections.Generic.GenericEqualityComparer1[[System.Int32,PublicKeyToken=b77a5c561934e089]]\b?System.Collections.Generic.KeyValuePair2[[System.Int32,PublicKeyToken=b77a5c561934e089]][]\0\0\0\t\0\0\0\0\0\0\t\0\0\0\0\0\0?System.Collections.Generic.GenericEqualityComparer1[[System.Int32,PublicKeyToken=b77a5c561934e089]]\0\0\0\0\a\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.KeyValuePair2[[System.Int32,PublicKeyToken=b77a5c561934e089]]?????System.Collections.Generic.KeyValuePair2[[System.Int32,PublicKeyToken=b77a5c561934e089]]\0\0\0keyvalue\0\0\b\b\0\0\0\0\v

您可能更喜欢使用自定义格式进行序列化,或者使用稍微轻一些的标准格式,例如JSON.

猜你在找的VB相关文章