2011-10-08 12 views
0

我想反序列化包含與ServiceStack.Text類嵌套列表的對象。ServiceStack.Text系列化異常(爲調用方法「太虛set_Item(的Int32,MyApp.MyClass)」提供的參數錯誤號碼)

序列化得很順利,但嘗試反序列化的時候,我得到這個錯誤:

System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Jsv.JsvReader 1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Jsv.JsvReader 1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Common.DeserializeList 2' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Jsv.JsvReader 1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Jsv.JsvReader 1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Common.DeserializeList 2' threw an exception. ---> System.TypeInitializationException: The type initializer for 'ServiceStack.Text.Jsv.JsvReader`1' threw an exception. ---> System.ArgumentException: Incorrect number of arguments supplied for call to method 'Void set_Item(Int32, MyApp.MyClass)'

我沒有在我的解決方案名爲set_Item方法,既不是我能找到一個在ServiceStack命名爲喜歡這種方法。文本彙編(使用反射器)。我沒有任何的問題可能是什麼線索......我需要一個快速的序列化的類型,但我知道最快(protobuf網)不支持嵌套列表。

任何想法?導致此問題的類是下面(的protomembers在那裏,因爲我測試的protobuf的方法)


[Serializable] 
/// <summary> 
/// Description of Livres. 
/// </summary> 
public class Livres : IEnumerable<Livre> 
{ 

    [ProtoMember(1)] 
    private List<Livre> listeLivres; 

    public List<Livre> ListeLivres 
    { 
     get { return listeLivres; } 
     set { listeLivres = value; } 
    } 

    public List<string> NomLivres 
    { 
     get 
     { 
      List<string> lst = new List<string>(); 
      foreach (Livre livre in this.listeLivres) 
      { 
       lst.Add(livre.NomLivre); 
      } 
      return lst; 
     } 
    } 

    public int Count 
    { 
     get 
     { 
      return ((this.listeLivres != null) ? this.listeLivres.Count : 0); 
     } 
    } 


    public Livre this[string nomLivre] 
    { 
     get 
     { 
      nomLivre = nomLivre.ToLower(); 
      if (nomLivre == "") 
      { 
       return null; 
      } 
      try 
      { 
       var u = this.listeLivres.Single(book => book.NomLivre.ToLower() == nomLivre); 
       return u; 
      } 
      catch (InvalidOperationException) 
      { 
       string pattern = "^[0-9][a-zA-Z]+$"; 
       Regex reg = new Regex(pattern); 
       if (reg.IsMatch(nomLivre)) 
       { 
        string nom = nomLivre[0].ToString() + " "; 
        nom += nomLivre.Substring(1).ToLower(); 
        try 
        { 
         var u = this.listeLivres.Single(book => book.NomLivre.ToLower() == nom); 
         return u; 
        } 
        catch (Exception) 
        { 
         return null; 
        } 
       } 
       else 
        return null; 
      } 
     } 
     set 
     { 
      if (nomLivre == "") 
      { 
       throw new 
        ArgumentNullException("L'index ne doit pas être une chaine vide."); 
      } 
      try 
      { 
       Livre liv = this.listeLivres.Single(book => book.NomLivre == nomLivre); 
       liv = value; 
      } 
      catch (InvalidOperationException ex) 
      { 
       string pattern = "^[0-9][a-zA-Z]+$"; 
       Regex reg = new Regex(pattern); 
       if (reg.IsMatch(nomLivre)) 
       { 
        string nom = nomLivre[0].ToString() + " "; 
        nom += nomLivre.Substring(1); 
        try 
        { 
         Livre L = this.listeLivres.Single(book => book.NomLivre == nom); 
         L = value; 
        } 
        catch (Exception e) 
        { 
         throw new ArgumentException("Ce nom de livre n'existe pas dans la liste", e); 
        } 
       } 
       else 
        throw new ArgumentException("Ce nom de livre n'existe pas dans la liste", ex); 
      } 
     } 
    } 
    /// <summary> 
    /// Obtient ou définit le Livre à l'index spécifié - 1 
    /// Exceptions: 
    /// ArgumentOutOfRangeException 
    /// </summary> 
    public Livre this[int index] 
    { 
     get 
     { 
      if (index < 1 || index > this.listeLivres.Count) 
      { 
       throw new 
        ArgumentOutOfRangeException("L'index spécifié n'était pas correct"); 
      } 
      return this.listeLivres[index-1]; 

     } 
     set 
     { 
      if (index < 1 || index > this.listeLivres.Count) 
      { 
       throw new 
        ArgumentOutOfRangeException("L'index spécifié n'était pas correct"); 
      } 
      this.listeLivres[index - 1] = value; 
     } 
    } 

    #region Constructeurs 
    public Livres() 
    { 
     this.listeLivres = new List<Livre>(); 
    } 
    public Livres(Livre livre) 
     : this() 
    { 
     this.listeLivres.Add(livre); 
    } 

    #endregion 
    /// <summary> 
    /// Retourne le verset correspondant si il existe, sinon null 
    /// Exceptions 
    /// ArgumentException 
    /// </summary> 
    /// <param name="referenceComplete">La référence du verset sous forme de chaine (ex: "1 Jean 5:19")</param> 
    public Verset GetVerset(string referenceComplete) 
    { 
     if (string.IsNullOrWhiteSpace(referenceComplete)) 
      return null; 
     string[] tab = referenceComplete.Split(); 
     try 
     { 
      string livre = ""; 
      int chapitre; 
      int verset; 
      if (tab.Length>2) 
      { 
       livre = tab[0]; 
      } 
      livre += tab[tab.Length -2]; 
      string [] tabVerse = tab[tab.Length -1].Split(':'); 
      chapitre = Convert.ToInt32(tabVerse[0]); 
      verset = Convert.ToInt32(tabVerse[1]); 
      return this[livre][chapitre][verset]; 
     } 
     catch (Exception ex) 
     { 
      throw new ArgumentException("Il n'y a aucun verset avec cette référence",ex); 
     } 
    } 

    public void Add(Livre livre) 
    { 
     this.listeLivres.Add(livre); 
    } 

    public static Livres operator +(Livres livres, Livre livre) 
    { 
     livres.Add(livre); 
     return livres; 
    } 

    public IEnumerator<Livre> GetEnumerator() 
    { 
     foreach (Livre item in this.listeLivres) 
     { 
      yield return item; 
     } 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.GetEnumerator(); 
    } 


    public void Serialize(string pathName= "BibleData.txt") 
    { 
     try 
     { 
      #region ServiceStack Serializer 
      TextWriter tw = new StreamWriter(pathName); 
      TypeSerializer<Livres> TypeSrlzr = new TypeSerializer<Livres>(); 
      TypeSrlzr.SerializeToWriter(this,tw); 
      //TypeSerializer.SerializeToWriter(this,tw); 
      tw.Close(); 
      #endregion 

     } 
     catch (Exception) 
     { 

      throw; 
     } 
    } 

    public static Livres Deserialize(string pathName= "BibleData.txt") 
    { 
     try 
     { 
      Livres Bible; 
      #region ServiceStack Deserializer 
      TextReader tr = new StreamReader(pathName); 

      TypeSerializer<Livres> typeSrlzr = new TypeSerializer<Livres>(); 
      Bible = typeSrlzr.DeserializeFromReader(tr); 
      //Bible = TypeSerializer<Livres>.DeserializeFromReader(tr); 
      #endregion 


      return Bible; 
     } 
     catch (Exception) { 

      throw; 
     } 
    } 


} 
+0

你可以發佈你試圖反序列化是造成錯誤的類的實例? – mythz

+0

@mythz好的我做到了 – GianT971

+0

今天我只有機會看到這個,不幸的是代碼並沒有完成,因爲它缺少了幾個類。 JsonSerializer不支持循環依賴,這可能是你的問題。 – mythz

回答

1

你不想從IEnumerable繼承,因爲它會在路上不太可能你的行爲期望。即所有Enumerables都像JSON數組/集合一樣被序列化,並且其他屬性被忽略。

只具有IEnumerable接口的問題是,有沒有辦法一般填充它。所以儘管你可能能夠序列化它,但是你將無法反序列化它。

,如果你實現一個集合接口,還提供了填充它的能力,即IList的<牛逼>ICollection的<牛逼>你會更成功。

+0

我有我所有的類實現IEnumerable實現IList,完成需要的功能,並且問題保持不變 – GianT971

1

我有同樣的問題。我需要我的類繼承IDataErrorInfo的,問題是在索引屬性。不過你可以自己修改源代碼。在OrmLiteConfigExtensions找到方法GetModelDefinition並查找:

GetValueFn = propertyInfo.GetPropertyGetterFn(), SetValueFn = propertyInfo.GetPropertySetterFn(),

更換GetValueFn線只與:

GetValueFn =( ?propertyInfo.GetIndexParameters()長度== 0)空:propertyInfo.GetPropertyGetterFn(),

這樣,它會跳過設定會拋出異常索引屬性的GetValueFn財產。

相關問題