1
votes

I am serializing an object with only properties being stored. It has a parent inheritance but I made sure the serialized attributes are of different indexes with the numbers.

[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;}
  }
}

and then 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;}
  }
 }

Now the really weird part is that the serialization seemingly is completely functional. Whenever I serialize and deserialize this "DesiredProto" it works, if I ignore the error. Lastly this isn't the full code snippet for these classes, they are much much longer, but hopefully the error is somehow contained in this.

1
what is DestinationType ? - Marc Gravell
Minor thing for info, but if you're using a recent version of C#, you may want to make use of automatically implemented properties - they save a lot of messing; for example: [ProtoMember(2)] public string Address {get;set;} - the compiler does basically exactly the same as you did (behind the scenes), but without the risk of making typos (using the wrong field, etc) - Marc Gravell
DestinationType is an enumeration! - jStaff
Great, I guessed right in my example code! - Marc Gravell
And thank you for the tip, I didn't know that those formats did the same thing! I am working with an "outdated" software solution. - jStaff

1 Answers

1
votes

Works fine here:

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

So: whatever the problem is, it doesn't show from your sample code. So the next step is to gradually introduce the context of your question until it starts breaking; then you'll know that the problem is in the last change you added.