.net 为什么反射报告System.Collections.Generic.Dictionary.KeyCollection对象是可序列化的?

.net 为什么反射报告System.Collections.Generic.Dictionary.KeyCollection对象是可序列化的?,.net,dictionary,serialization,reflection,.net,Dictionary,Serialization,Reflection,我在.NET4.5.2中工作 根据,字典(TKey,TValue).KeyCollection类未标记为可序列化。在反思中,它的报告是这样的 真正的问题: 我有一个自定义字典,它继承了泛型.NET字典并实现自定义序列化。试图将信息打包到SerializationInfo对象中,它会附加Keys集合。因为它被标记为可序列化(我们的自定义序列化程序的工作方式与我的示例代码不同,但产生相同的结果),所以它尝试将序列化对象附加到集合中。当BinaryFormatter尝试序列化KeyCollection

我在.NET4.5.2中工作

根据,字典(TKey,TValue).KeyCollection类未标记为可序列化。在反思中,它的报告是这样的

真正的问题:
我有一个自定义字典,它继承了泛型.NET字典并实现自定义序列化。试图将信息打包到SerializationInfo对象中,它会附加Keys集合。因为它被标记为可序列化(我们的自定义序列化程序的工作方式与我的示例代码不同,但产生相同的结果),所以它尝试将序列化对象附加到集合中。当BinaryFormatter尝试序列化KeyCollection时,它调用

.GetObjectData(System.Runtime.Serialization.SerializationInfo信息,System.Runtime.Serialization.StreamingContext上下文)

方法,而不是密钥集合。它陷入了一个试图序列化集合的循环中

你知道为什么它报告它是可序列化的,或者为什么二进制格式化程序调用超类上的GetObjectData方法而不是引用的对象吗

谢谢

我把这个贴到了connect.microsoft.com上,因为我觉得这个行为是框架中的一个bug

复制异常的简单程序

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters
Imports System.Runtime.Serialization.Formatters.Binary

Public Module Program

  Public Sub Main()
    Dim x As New CustomDictionary(Of String, Integer)
    x.Add("TestItem1", 1)
    Console.WriteLine("Keys Collection Serializable? {0}", x.Keys.GetType.IsSerializable)
    Dim b() As Byte = SerializeSettingsToByteArray(x)
  End Sub

  Public Function SerializeSettingsToByteArray(obj As Object) As Byte()
    Try
      Console.WriteLine(String.Format("Serializing {0} Object", obj.GetType().Name))

      Dim z As New BinaryFormatter()
      z.AssemblyFormat = FormatterAssemblyStyle.Full

      Dim m As New MemoryStream()
      z.Serialize(m, obj)

      Dim b() As Byte
      ReDim b(CInt(m.Length - 1))
      m.Seek(0, SeekOrigin.Begin)
      Dim readCount As Integer = m.Read(b, 0, CInt(m.Length))
      Return b
    Catch ex As Exception
      Throw
    End Try
  End Function

End Module

<Serializable>
Public Class CustomDictionary(Of TKey, TValue)
  Inherits Dictionary(Of TKey, TValue)
  Implements ISerializable

  Public Sub New()
  End Sub

  Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext)
    'Not serializing everything for brevity.
    info.AddValue("Keys", SerializeSettingsToByteArray(Me.Keys))
  End Sub
End Class
Imports System.IO
导入System.Runtime.Serialization
导入System.Runtime.Serialization.Formatters
导入System.Runtime.Serialization.Formatters.Binary
公共模块程序
公用分干管()
Dim x作为新的CustomDictionary(字符串,整数)
x、 添加(“测试项目1”,1)
Console.WriteLine(“密钥集合可序列化?{0}”,x.Keys.GetType.IsSerializable)
Dim b()作为字节=SerializeSettingsToByteArray(x)
端接头
公共函数序列化SettingsToByteArray(obj作为对象)作为字节()
尝试
WriteLine(String.Format(“序列化{0}对象”,obj.GetType().Name))
Dim z作为新的二进制格式化程序()
z、 AssemblyFormat=FormatterAssemblyStyle.Full
Dim作为新的内存流()
z、 序列化(m,obj)
Dim b()作为字节
雷迪姆b(CInt(米长度-1))
m、 搜索(0,SeekOrigin.Begin)
作为整数的Dim readCount=m.Read(b,0,CInt(m.Length))
返回b
特例
扔
结束尝试
端函数
端模块
公共类CustomDictionary(属于TKey、TValue)
继承字典(TKey、TValue的)
实现ISerializable
公共分新()
端接头
Public重写子GetObjectData(信息作为SerializationInfo,上下文作为StreamingContext)
“不是为了简洁而序列化所有内容。
info.AddValue(“键”,序列化设置为字节数组(Me.Keys))
端接头
末级