如何使用 protobuf .NET 序列化接口类型成员?

发布于 2024-09-06 09:59:29 字数 765 浏览 2 评论 0原文

以下测试失败并出现此错误:

“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 技术交流群。

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

发布评论

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

评论(1

素年丶 2024-09-13 09:59:29

这是基于契约的序列化器的一个共同特征,包括 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:

  • during deserialization, what type would it create for A.B?
  • during serialization, the "what is the current object" bears little relationship to the contract
    • in particular it gets very messy if the type implements multiple interfaces

This is a scenario I want to get something working for in "v2" though (but maybe not quite for release); I'm thinking:

  • either A.B must be non-null to start with (i.e. A decides the type of A.B), or a default implementation must be specified somewhere
  • interface-based is mutually exclusive vs. inheritance; when using interfaces there can be no inheritance support
  • all interface usage would be via properties, never fields (obviously)

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

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