protobuf-net 的抽象/数组问题
我在尝试反序列化从同一抽象基类派生并使用AsReference进行序列化的对象数组时遇到问题。以下重现了我的问题:
[ProtoContract]
[ProtoInclude(1,typeof(A))]
[ProtoInclude(2, typeof(B))]
public abstract class Base
{}
[ProtoContract]
public class A:Base
{
[ProtoMember(1)]
public int J
{
get;
set;
}
}
[ProtoContract]
public class B : Base
{ }
[ProtoContract]
public class Obj
{
[ProtoMember(1,AsReference=true,DynamicType=true)]
public Base[] array = new Base[] { new A(), new B() };
}
然后尝试像这样进行反/序列化:
Obj bob = new Obj();
using (MemoryStream m = new MemoryStream())
{
Serializer.Serialize(m, bob);
m.Position = 0;
var clone = Serializer.Deserialize<Obj>(m);
}
根据“Obj.array”的 ProtoMember 选项,反序列化时出现以下异常之一:
- [ProtoMember(1,AsReference=true,DynamicType=true)] (如上所述):System.RuntimeType.TryChangeType 处的参数异常。即使 Base 不是抽象的
- [ProtoMember(1,DynamicType=true)] ,也会发生这种情况:System.RuntimeType.TryChangeType 处的参数异常。即使 Base 不是抽象的
- [ProtoMember(1,AsReference=true)] 也会发生这种情况:“无法创建抽象类”异常。如果 Base 不是抽象的,我会得到“对象与目标类型不匹配”。例外。
- [ProtoMember(1)] :也不例外,但我需要序列化事物作为参考(另外,反序列化的 Obj 在“数组”中有四个项目,但我认为这个错误已经被报告)
我需要能够序列化使用 AsReference 的数组...如果我没有做错什么,有人能想到一个合适的解决方法吗? 谢谢!
I'm having trouble trying to deserialise an array of objects which derive from the same abstract base class and serialised with AsReference on. The following reproduces my issue:
[ProtoContract]
[ProtoInclude(1,typeof(A))]
[ProtoInclude(2, typeof(B))]
public abstract class Base
{}
[ProtoContract]
public class A:Base
{
[ProtoMember(1)]
public int J
{
get;
set;
}
}
[ProtoContract]
public class B : Base
{ }
[ProtoContract]
public class Obj
{
[ProtoMember(1,AsReference=true,DynamicType=true)]
public Base[] array = new Base[] { new A(), new B() };
}
Then attempt to de/serialise like so:
Obj bob = new Obj();
using (MemoryStream m = new MemoryStream())
{
Serializer.Serialize(m, bob);
m.Position = 0;
var clone = Serializer.Deserialize<Obj>(m);
}
Depending on the ProtoMember options for 'Obj.array' I get one of the following Exceptions upon deserialising:
- [ProtoMember(1,AsReference=true,DynamicType=true)] (as written above): Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
- [ProtoMember(1,DynamicType=true)] : Argument Exception at System.RuntimeType.TryChangeType. This occurs even if Base is not abstract
- [ProtoMember(1,AsReference=true)] : 'Cannot create an abstract class' exception. If Base is not abstract, I get an "Object does not match target type." exception.
- [ProtoMember(1)] : No exception, but I need to serialise things as a reference (also, the deserialised Obj has FOUR items in 'array', but I think this bug has already been reported)
I need to be able to serialise the array using AsReference... If I'm not doing something wrong, can anyone think of a decent workaround for this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论