![](/img/trans.png)
[英]protobuf-net skips a member (of Interface type) from base class
[英]Serialize a class having an interface type member using protobuf-net
我無法使用protobuf-net序列化我的課程,問題似乎是protobuf-net無法序列化接口。
interface MyInterface
{
string name;
}
[ProtoContract]
[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]
public abstract class ParentClass
{
[ProtoMember(1)]
List<MyInterface> elements;
}
[ProtoContract]
public class MyClass1 : ParentClass, MyInterface
{
[ProtoMember(1)]
int x;
}
[ProtoContract]
public class MyClass2 : MyInterface
{
[ProtoMember(1)]
string y;
}
我無法序列化任何MyClass1類型的對象,因為元素是接口列表,可以是Mylass1或MyClass2。 我收到一些編碼未設置錯誤。
誰能讓我知道如何解決這個問題。 謝謝。
在當前的正式版本中,我不包括接口序列化支持。 但是,我確實提供了(來自另一個用戶的)補丁程序,似乎可以實現這一目的。
我還沒有將此補丁應用到內核,僅僅是因為我需要先專注於完成“ v2”,然后再添加更多功能(尤其是該功能需要針對v2完全重新實現),但是我很高興可以與您共享補丁。
或者:使用基類而不是接口。 即 (通過支持[ProtoInclude]
-不過,您的事實MyClass1
已經有一個父類有點事情復雜化。
編輯:v2現在支持此功能。 顯然,該代碼必須了解預期的具體實現-但現在可以將包含的內容附加到接口上(或可選地在原始POCO模型的代碼中指定)。
我的猜測是您需要添加:
[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]
MyClass1
和MyClass2
都可以MyClass1
,因為您繼承自MyInterface
,而序列化將不知道類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.