Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 已添加具有相同密钥的项。在c中反序列化对象时#_C# - Fatal编程技术网

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

C# 已添加具有相同密钥的项。在c中反序列化对象时#,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,

我正在反序列化一个对象,得到了标题错误。暗号

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];
public LinkedList allPeople=new LinkedList();
public LinkedList allDeadPeople=new LinkedList();
public LinkedList allTrees=new LinkedList();
公共列表所有建筑物=新列表();
public List allLargeTools=new List();
public List allCollectedItems=new List();
public List allBoar=新列表();
public LinkedList allAntelope=新LinkedList();
public LinkedList all=new LinkedList();
public List allOre=新列表();
public List allHistory=新列表();
public Boolean Completed=true;
[非串行化]
公共插座;
[非串行化]
公共列表传输缓冲区=新列表();
[非串行化]
公共字节[]缓冲区=新字节[5500000];
如果你需要更多的信息,我可以提供。我想这是件简单的事,但我就是做不出来。当然,这可能与字典中的重复键有关,如果是这样的话,如何进一步深入了解这一点

提前谢谢你的帮助


Scott

尝试暂时序列化和反序列化单个成员,而不是整个类,以缩小哪个成员包含具有重复键的词典,以便您可以将注意力集中在较小的区域。

您是否在某处使用集合?
System.Collections.Generic.HashSet
Iesi.Collection.Generic
set类(O/R映射器NHibernate附带的类)都很麻烦。我切换到
IList来解决这个问题。

很抱歉,这与对象的序列化无关,但我当然是在创建新对象,然后将结构从字典更改为列表,然后在其他一些代码中错误地声明了对象的重复实例

对不起,错过了信息


Scott

有趣的想法,你到底要怎么做?此外,没有字典反序列化,这怎么可能?@Scott 1)当你序列化对象时,与其序列化你的SendWorldDataInstance,不如序列化你的SendWorldDataInstance.allPeople。反序列化时,将结果强制转换为LinkedList。对每个成员重复此操作,查看是否只有其中一个成员导致了错误。2) 必须是其中一个类的序列化逻辑在内部使用字典。问题是否只发生在一个特定的序列化数据集上,或者您可以通过创建一些新对象、序列化它,然后反序列化它来重现它?如果不能以这种方式重新创建,那么序列化的数据可能与当前版本的代码不同步。