使用 Protobuf-net 和继承时有没有办法纠正这个运行时错误?

发布于 2024-10-17 04:57:40 字数 797 浏览 1 评论 0原文

好的,我有以下代码,以前可以工作,但现在不行。唯一改变的是我现在使用 VS2010 和 .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
}

我在这里缺少什么吗?

当我覆盖使用相同标签号的父属性时,我可以看到 PB 对两个类的相同标签号感到厌烦,但我认为这会被隔离......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-10-24 04:57:40

我将不得不调查 - 大概是属性和覆盖的一些细微差别 - 我不知道具体的变化,但这是......意想不到的。

作为试用,您可以在覆盖字符串 MyString 上添加 [ProtoIgnore] - 但请验证它是否仍然序列化它(从基本类型)!

即,

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

作为信息,我已经针对 v2 测试了您的代码,并且它在针对 .NET 4.0 的 VS2010 中完美运行;我这台机器上没有v1...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文