【发布时间】: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