1

在尝试反序列化由 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: 
4

0 回答 0