c# - 已添加具有相同 key 的项目。在 C# 中反序列化对象时

标签 c#

我正在反序列化一个对象,并收到标题错误。代码如下

byte[] dataBuffer = TransmissionBuffer.ToArray();
dataBuffer = Decompress(dataBuffer);
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
mem.Write(dataBuffer, 0, dataBuffer.Length);
mem.Seek(0, 0);

return (SendWorldData)bin.Deserialize(mem);

返回行上出现错误。

这是剪贴板异常

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 TheDarkAgesReturn.SendWorldData.DeSerialize() in C:\SVNProjects\Dark Ages Return\TheDarkAgesReturn2008NET3.5V1.5\TheDarkAgesReturn\SendWorldData.cs:line 64
       at TheDarkAgesReturn.ClientDarkAges.Done(SendWorldData status) in C:\SVNProjects\Dark Ages Return\TheDarkAgesReturn2008NET3.5V1.5\TheDarkAgesReturn\ClientDarkAges.cs:line 125
       at TheDarkAgesReturn.ClientDarkAges.Receive(IAsyncResult result) in C:\SVNProjects\Dark Ages Return\TheDarkAgesReturn2008NET3.5V1.5\TheDarkAgesReturn\ClientDarkAges.cs:line 108
       at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
       at System.Net.ContextAwareResult.CompleteCallback(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.ContextAwareResult.Complete(IntPtr userToken)
       at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
       at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  InnerException: 

类结构如下;

public LinkedList<Person> allPeople = new LinkedList<Person>();
public LinkedList<Person> allDeadPeople = new LinkedList<Person>();
public LinkedList<Plant> allTrees = new LinkedList<Plant>();
public List<Building> allBuildings = new List<Building>();
public List<LargeTool> allLargeTools = new List<LargeTool>();
public List<CollectedRawMaterial> allCollectedItems = new List<CollectedRawMaterial>();
public List<Boar> allBoar = new List<Boar>();
public LinkedList<Animal> allAntelope = new LinkedList<Animal>();
public LinkedList<Animal> allChickens = new LinkedList<Animal>();
public List<Deposit> allOre = new List<Deposit>();
public List<History> allHistory = new List<History>();
public Boolean Completed = true;

[NonSerialized]
public Socket Socket;
[NonSerialized]
public List<byte> TransmissionBuffer = new List<byte>();
[NonSerialized]
public byte[] buffer = new byte[5500000];

如果您需要更多信息,我可以提供。我希望这很简单,但我就是无法解决。当然,这可能与字典中的重复键有关,如果是的话,如何进一步深入找出这一点。

感谢您提前提供的所有帮助。

斯科特

最佳答案

你在某处使用集合吗?两者都是System.Collections.Generic.HashSet<T>Iesi.Collection.Generic设置类(那些与 O/R 映射器 NHibernate 一起提供的类)很麻烦。我切换到 IList<T>解决该问题。

关于c# - 已添加具有相同 key 的项目。在 C# 中反序列化对象时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5127133/

相关文章:

c# - CosmosDB System.ArgumentNullException : 'Value cannot be null. (Parameter ' authKeyOrResourceToken')'

c# - 如何在 ASP.NET 中使用具有 HTML 正文 + 附件的 GMAIL API 发送电子邮件

c# - 如何在我的应用程序中集成实时 Skype 状态?

c# - 在 Python 中获取 C# 日志

C# GC.Collect() 和内存

c# - 做编译绑定(bind)(x :Bind) require the ViewModel to derive from DependencyObject?

c# - 如何旋转 4x4 矩阵 (C#/XNA)

c# - EF5 多对多更新断开连接的场景

c# - HttpWebRequest 与 HttpClient

c# - LINQ 表达式 : Specifing maximum groupby size