【问题标题】:How to declare that a type is implementing an interface and a base class in ProtoBuf?如何在 ProtoBuf 中声明一个类型正在实现一个接口和一个基类?
【发布时间】:2017-12-25 17:33:34
【问题描述】:

我们的模型中有一个案例如下所示:

[ProtoContract]
public interface ISomeInterface{
    //...
}

[ProtoContract]
[ProtoInclude(100,typeof(SomeImplementation)]
public class SomeRootClass {
    //...
}

[ProtoContract]
public class SomeImplementation: SomeRootClass,ISomeInterface{
    //...
}

在某个时候,我们有一个 SomeImplementation 的实例,它在类中被引用:

[ProtoContract]
public class SomeClassWithInterfaceUsage{
    [ProtoMember(1)]
    public ISomeInterface SomeReference{get;set;}
}

当我们尝试序列化时,我们有这个错误:

System.InvalidOperationException : 无法准备一个 序列化程序:SomeRootClass ----> System.InvalidOperationException :没有为类型定义序列化程序:ISomeInterface

所以我把界面改成如下:

[ProtoContract]
[ProtoInclude(100,typeof(SomeImplementation)]
public interface ISomeInterface{
    //...
}

但现在我有这个错误:

System.InvalidOperationException : A type can only participate in one inheritance hierarchy

我应该如何处理这种情况?

【问题讨论】:

  • 你真的很喜欢复杂的边缘场景,不是吗 :) 你有没有考虑过使用简单的 DTO 模型进行序列化,而不是试图强制序列化程序针对一个复杂的模型工作不合适吗?
  • 嗯,这是我现在遇到的问题,没有当前模型(这不是 DTO)。如果它不意味着复制和考虑我们当前模型的 500 个类,我会选择 DTO 路径。维护将是一场噩梦

标签: c# .net serialization protocol-buffers protobuf-net


【解决方案1】:

据我所知,这不受支持,请在此处查看标记答案:enter link description here

我最终只声明了类型,并使用 ProtoMemberAttribute 上带有 DynamicType 标志的属性的对象

【讨论】:

  • 我赞同这个答案。
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多