如何使用 protobuf .NET 序列化接口类型成员?
以下测试失败并出现此错误:
“System.InvalidOperationException:否 找到合适的默认 IB 编码。”
[ProtoContract]
public class A
{
[ProtoMember(1)]
public IB B { get; set; }
}
public interface IB
{
}
[ProtoContract]
public class B : IB
{
[ProtoMember(1)]
public int SomeProperty { get; set; }
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test()
{
var a = new A {B = new B()};
using (var m = new MemoryStream())
{
Serializer.Serialize(m, a);
}
}
}
我正在使用 Protobuf.net 的实现:
http:// /code.google.com/p/protobuf-net/
我错过了什么吗?非常感谢。
The following test fails with this error:
"System.InvalidOperationException : No
suitable Default IB encoding found."
[ProtoContract]
public class A
{
[ProtoMember(1)]
public IB B { get; set; }
}
public interface IB
{
}
[ProtoContract]
public class B : IB
{
[ProtoMember(1)]
public int SomeProperty { get; set; }
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test()
{
var a = new A {B = new B()};
using (var m = new MemoryStream())
{
Serializer.Serialize(m, a);
}
}
}
I'm using this implementation of Protobuf.net :
http://code.google.com/p/protobuf-net/
Did I miss something? thanks you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是基于契约的序列化器的一个共同特征,包括 XmlSerializer 等(即那些不包含每个对象的类型元数据的序列化器)。
有一些事情让这个问题变得棘手:
AB
创建什么类型?这是我希望在“v2”中得到一些工作的场景(但可能不完全适合发布);我在想:
AB
必须为非空(即A
决定AB
的类型),或者默认实现必须在某处指定或者,也许更适合所呈现的场景,我们可以使用类似
[ProtoInclude]
的东西来指示具体类型。但在这些限制内,我认为有些事情是可能的。但今天不行。
That is a common feature of contract-based serializers, including
XmlSerializer
, etc (i.e. those that don't include type metadata for every object).There are a few things that make this tricky:
A.B
?This is a scenario I want to get something working for in "v2" though (but maybe not quite for release); I'm thinking:
A.B
must be non-null to start with (i.e.A
decides the type ofA.B
), or a default implementation must be specified somewhereAlternatively, and perhaps more suited to the scenario presented, we could use something like
[ProtoInclude]
to indicate the concrete types.But within those limits I think something is possible. But not today.