1

私はSilverlightを初めて使用します。私は、主にシリアライゼーションとデシリアライゼーションに依存するプロジェクトに取り組んでいます。

以前は、WPF の場合、シリアライズ可能なクラスに慣れていました。Silverlight の場合、protobuf が非常に役立つことがわかりました。しかし、私はこの例外に悩まされています。この問題の原因はわかりません。私を助けてください。

Silverlight 3.0 を使用しています。protobuf ネット r282

私が使用しているコードを見つけてください。

[ProtoContract]
public class Report
{
    public Report()
    {
    }

    [ProtoMember(1)]
    public SubReports SubReports { get; set; }
}

[ProtoContract]
public class SubReports
   : List<SubReport>
{
    public SubReports()
    {
    }

    [ProtoMember(1)]
    public SubReport SubReport { get; set; }
}

[ProtoContract]
public class SubReport
{
    public SubReport()
    {
    }

    [ProtoMember(1)]
    public string Name { get; set; }
}

逆シリアル化に使用しているコードは

    public static T Deserialize<T>(Byte[] bytes) where T
        : Report
    {
        return ProtoBuf.Serializer.Deserialize<T>(new MemoryStream(bytes));
    }

私のサンプル XML は次のようになります

Report  
   ...SubReports  
      ...SubReport Name=”Q1 Report”   
      ...SubReport Name=”Q2 Report”   
      ...SubReport Name=”Q3 Report”   
      ...SubReport Name=”Q4 Report”     

前もって感謝します。
ヴィノード

4

1 に答える 1

1

(注:「グループタグ」の問題を再現できませんでした。これに関する私の最初の考えについては、編集履歴を参照してください。現在は削除されています。これを再現するのを手伝ってもらえれば、私は感謝しています)

問題はSubReports. これをリストシリアル化エンティティ ( )の両方として定義しました。後者が優先されるため、リスト上の単一のサブレポートをシリアル化しようとしていました (これは常に?)。[ProtoContract]null

これを次のように変更すると:

// note no attributes, no child property
public class SubReports : List<SubReport> { }

または、完全に削除して作成Report.SubReportsすると、List<SubReport>正常に動作するはずです。以下の作品:

static void Main() {
    byte[] blob;
    // store a report
    using (MemoryStream ms = new MemoryStream()) {
        Report report = new Report {
            SubReports = new List<SubReport> {
                new SubReport { Name="Q1"}, 
                new SubReport { Name="Q2"},
                new SubReport { Name="Q3"},
                new SubReport { Name="Q4"},
            }
        };

        Serializer.Serialize(ms, report);
        blob = ms.ToArray();
    }
    // show the hex
    foreach (byte b in blob) { Console.Write(b.ToString("X2")); }
    Console.WriteLine();

    // reload it
    using (MemoryStream ms = new MemoryStream(blob)) {
        Report report = Serializer.Deserialize<Report>(ms);
        foreach (SubReport sub in report.SubReports) {
            Console.WriteLine(sub.Name);
        }
    }
}

ブロブの表示:

0A040A0251310A040A0251320A040A0251330A040A025134

于 2010-02-10T11:53:28.697 に答える