使用 Protobuf-net 和继承时有没有办法纠正这个运行时错误?
好的,我有以下代码,以前可以工作,但现在不行。唯一改变的是我现在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将不得不调查 - 大概是属性和覆盖的一些细微差别 - 我不知道具体的变化,但这是......意想不到的。
作为试用,您可以在
覆盖字符串 MyString
上添加[ProtoIgnore]
- 但请验证它是否仍然序列化它(从基本类型)!即,
作为信息,我已经针对 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 theoverride string MyString
- but please verify that it still serializes it (from the base-type)!i.e.
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...