[英]An item with the same key has already been added. When Deserialize an object in 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>
來解決此問題。
嘗試臨時序列化和反序列化單個成員而不是整個類,以縮小哪個成員包含具有重復鍵的字典,以便將注意力集中在較小的區域。
抱歉,但這與對象的序列化無關,但是我當然是在創建新對象,然后將結構從字典更改為列表,然后我重復了對象的實例,因為我在某些其他代碼中錯誤地聲明了它們。
對不起,小姐信息!
斯科特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.