Stécy
Stécy

Reputation: 12339

Is there a way to correct this runtime error when using Protobuf-net and inheritance?

Ok, I have the following code that used to work but now it does not. The only thing that changed is now I'm using VS2010 and .NET4

[ProtoContract]
[ProtoInclude(1, typeof(DerivedClass))]
public abstract class BaseClass
{
    [ProtoMember(2)]
    protected virtual string MyString { get; set; }
}

[ProtoContract]
public class DerivedClass : BaseClass
{
    [ProtoMember (2)]
    public readonly int SomeInt = 10;

    protected override string MyString
    {
        get { return "dummy"; }
        set { base.MyString = value; }
    }
}

[Test]
public void Test()
{
    var derived = new DerivedClass();


    using (Stream s = new MemoryStream ())
        Serializer.Serialize(s, derived);   // InvalidOperationException: Duplicate tag 2 detected in SomeInt
}

Is there something I am missing here?

I can see that PB is barfing on the same tag number for both classes when I override the parent property that uses the same tag number but I thought that would be isolated...

Upvotes: 1

Views: 312

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062502

I will have to investigate - presumably some nuance of attributes and overrides - I don't know of a specific change, but that is... unexpected.

As a trial, you could add [ProtoIgnore] on the override string MyString - but please verify that it still serializes it (from the base-type)!

i.e.

[ProtoIgnore]
protected override string MyString
{
    get { return "dummy"; }
    set { base.MyString = value; }
}

For info, I've tested your code against v2, and it worked perfectly in VS2010 targeting .NET 4.0; I don't have v1 handy on this machine...

Upvotes: 1

Related Questions