簡體   English   中英

protobuf-net是否支持多接口繼承

[英]does protobuf-net support multiple interface inheritance

我認為我的問題與這個問題非常相似: Protobuf-net使用接口和抽象基類創建類型模型,但是Marc在此給出的解決方案從本質上將抽象類和接口的多重繼承降低為單個繼承設計。

對我來說,問題是,我實際上需要像這樣的多個接口繼承:

interface ITestBase 
{
}
abstract class TestBase : ITestBase 
{
}
class TestTypeA : TestBase, ITestTypeA 
{
}
interface ITestTypeA 
{
}
class TestTypeB : TestBase, ITestTypeB 
{
}
interface ITestTypeB 
{
}

在這里,我無法通過使TestBase實現ITestTypeA或ITestTypeB(這是另一個問題的解決方案)來簡化這個問題,因為具體的類TestTypeA應該同時實現ITestTypeA和ITestBase,而TestTypeB應該實現ITestTypeB和ITestBase。

我正在使用protobuf-net v2.0.0.480

我找到了這個可行的解決方案。 不知道是否建議使用它,或者它是否會在運行時在不知不覺中損壞,但到目前為止,它似乎可以用於我的測試。

因為protobuf-net將接口像對待串行化的具體類一樣對待,所以它遇到了多個繼承問題(這是我所了解的),因此我所做的只是從一個基類繼承而未指定任何類與其接口之間的關系。

並創建一個可用於定義類層次結構的具體基類,如下所示。

[ProtoContract]
interface ITestBase 
{
}

[ProtoContract]
[ProtoInclude(1, typeof(TestTypeA))]
[ProtoInclude(2, typeof(TestTypeB))]
abstract class TestBase : ITestBase
{
}

[ProtoContract]
class TestTypeA : TestBase, ITestTypeA 
{
}

[ProtoContract]
interface ITestTypeA 
{
}

[ProtoContract]
class TestTypeB : TestBase, ITestTypeB 
{
}

[ProtoContract]
interface ITestTypeB 
{
}

實際上,界面前面的所有[ProtoContract]甚至都可能無關緊要。 我發現將它們全部消除似乎也可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM