2016-04-26 10 views
1

プロパティのみが格納されたオブジェクトをシリアル化しています。 それは親の継承を持っていますが、シリアル化された属性が数値と異なるインデックスであることを確認しました。ProtoBuf-Net:タイプ用にシリアライザが定義されていません:System.Object

[ProtoContract] 
[ProtoInclude(597, typeof(DesiredProto))] 
[ProtoInclude(598, typeof(RandomClass1Proto))] 
[ProtoInclude(599, typeof(RandomClass2Proto))] 
[ProtoInclude(600, typeof(RandomClass3Proto))] 
public class BaseProto 
{ 
    protected string mName = ""; 
    protected string mOwner = ""; 
    protected VObjectType mVType; //this is an enumeration! 
    public BaseProto(){} 

    [ProtoMember(1)] 
    public String Name 
    { 
    get { return mName; } 
    set { mName = value;} 
    } 

    [ProtoMember(2)] 
    public String Owner 
    { 
    get { return mOwner; } 
    set { mOwner = value;} 
    } 

    [ProtoMember(3)] 
    public VObjectType VType 
    { 
    get { return mVType; } 
    set { mVType = value;} 
    } 
} 

、その後DesiredProto:

[ProtoContract] 
public class DesiredProto : BaseProto 
{ 
    protected DestinationType mDestType; 
    protected string mAddress = ""; 

    public DesiredProto() 
    { 
    } 

    [ProtoMember(1)] 
    public DestinationType DestType //this is an enumeration 
    { 
    get { return mDestType; } 
    set { mDestType = value;} 
    } 

    [ProtoMember(2)] 
    public String Address 
    { 
    get { return mAddress; } 
    set { mAddress = value;} 
    } 
} 

は今本当に奇妙な部分は、シリアライズは一見完全に機能していることです。この「DesiredProto」をシリアライズして逆シリアル化すると、エラーが無視されます。 最後に、これはこれらのクラスの完全なコードスニペットではなく、はるかに長いですが、うまくいけばエラーが何とかこの中に含まれています。

+0

'DestinationType'とは何ですか? –

+0

情報のためのマイナーなことですが、C#の最新バージョンを使用している場合は、自動的に実装されたプロパティを使用することができます。 '[ProtoMember(2)] public string Address {get; set;}' - コンパイラは基本的にあなたが行ったのと全く同じですが(誤っている)フィールドなど) –

+0

DestinationTypeは列挙型です! – jStaff

答えて

1

は罰金ここワークス:

using ProtoBuf; 
using System; 

class Program 
{ 
    static void Main() 
    { 
     BaseProto obj = new DesiredProto 
     { 
      Address = "123 Somewhere", 
      DestType = DestinationType.Foo, 
      Name = "Marc", 
      Owner = "Also Marc", 
      VType = VObjectType.A 
     }; 
     BaseProto clone = Serializer.DeepClone(obj); 
     DesiredProto typedClone = (DesiredProto)clone; 
     Console.WriteLine(typedClone.Address); 
     Console.WriteLine(typedClone.DestType); 
     Console.WriteLine(typedClone.Name); 
     Console.WriteLine(typedClone.Owner); 
     Console.WriteLine(typedClone.VType); 
    } 
} 

public enum DestinationType { Foo } // I just made a guess here 
public enum VObjectType // you said this is an enum 
{ 
    A, B, C 
} 
class RandomClass1Proto : BaseProto { } // just a dummy type to make it complile 
class RandomClass2Proto : BaseProto { } 
class RandomClass3Proto : BaseProto { } 

// omitted: code from the question here 

だから

:問題が何であれ、それはあなたのサンプルコードから表示されません。だから次のステップは、あなたの質問の文脈が徐々に崩壊し始めるまで段階的に導入することです。問題が最後に追加した変更であることがわかります。

関連する問題