如何在二进制格式化反序列化过程中跟踪重复键异常的原因?

时间:2012-06-04 02:24:40

标签: c# .net serialization deserialization binaryformatter

在尝试反序列化由BinaryFormatter创建的文件时,会抛出以下异常:

ArgumentException:已添加具有相同键的项目。

堆栈跟踪从BinaryFormatter中的Deserialize方法开始,我无法识别代码中的哪个类导致了冲突。

我正在序列化100多个标有 [Serializable] 属性

的类

我看到另一个问题表明原因可能与名称重复的字段有关。但是,除了手工梳理代码之外,我无法在100多个类中找到引起碰撞的字段,并且不知道更有效的方法来跟踪它。

除了手动遍历每个班级的字段外,还有更好的方法来开始调试此问题吗?首先可能有不同的碰撞原因吗?我非常感谢能够提供这个问题的任何见解。

例外内容

System.ArgumentException was unhandled
  Message=An item with the same key has already been added.
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Collections.Generic.Dictionary`2.OnDeserialization(Object sender)
   at System.Runtime.Serialization.DeserializationEventHandler.Invoke(Object sender)
   at System.Runtime.Serialization.ObjectManager.RaiseDeserializationEvent()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at SPX.Saves.Save`1.Read() in .\SPX.Save\Save.cs:line 42
   at OGUR.States.MainMenuState.Update() in .\OGUR\OGUR\States\MainMenuState.cs:line 64
   at SPX.States.StateManager.Update() in .\SPX.States\StateManager.cs:line 30
   at OGUR.Game.Update(GameTime gameTime) in .\OGUR\OGUR\Game.cs:line 63
   at Microsoft.Xna.Framework.Game.Tick()
   at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
   at Microsoft.Xna.Framework.GameHost.OnIdle()
   at Microsoft.Xna.Framework.WindowsGameHost.RunOneFrame()
   at Microsoft.Xna.Framework.WindowsGameHost.ApplicationIdle(Object sender, EventArgs e)
   at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Microsoft.Xna.Framework.WindowsGameHost.Run()
   at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
   at Microsoft.Xna.Framework.Game.Run()
   at OGUR.EntryPoint.Main(String[] args) in .\OGUR\OGUR\EntryPoint.cs:line 15
InnerException: 

0 个答案:

没有答案