2015-12-21 25 views
0

這是 How to set [DataMember] on all class membersDataContract序列錯誤

延續所以我必須序列與字典和其他成員的類。

我已經chonse DataContext的系列化富硒

public SimpleDataGridSample() 
    { 
     if (false) 
     { 

     MyClass theclass = new MyClass(); 

     var serializer = new DataContractSerializer(typeof(MyClass)); 

     using (Stream fileStream = File.Open("aaa.bin", FileMode.Create)) 
     { 
      XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(fileStream); 
      serializer.WriteObject(binaryDictionaryWriter, theclass); 
      binaryDictionaryWriter.Flush(); 
     } 
     } 
     else 
     { 
     MyClass theclass; 
     var serializer = new DataContractSerializer(typeof(MyClass)); 

     using (Stream fileStream = File.Open("aaa.bin", FileMode.Open)) 
     { 
      XmlDictionaryReaderQuotas xq = new XmlDictionaryReaderQuotas(); 
      XmlDictionaryReader binaryDictionarReader = XmlDictionaryReader.CreateBinaryReader(fileStream, xq); 
      theclass = (MyClass)serializer.ReadObject(binaryDictionarReader); 

     } 
     } 

    } 
    } 

和工作。

但這只是一個測試程序。 應用於我的類,它是比較複雜的。當我得到這個錯誤:

{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"} 

不明白什麼指數在談論。

主要類由多個成員(也是字典和可觀察列表)和其他子類組成。 每一個類被標記[DataContract(IsReference = true)] 一個成員被標記[DataContext]

感謝名單,這是不可能的其他用戶aswer

回答

0

確定。起初我不明白問題出在哪裏,因爲我之前從未使用過使用過的DataContract序列化,並且對此並不感冒。但它的作品! 讓我走上正軌的是好老的智能感知。 這是創建後的變量,它包含異常。 謝謝你

enter image description here