1

I think my question is very similar to this question: Protobuf-net creating typemodel with interface and abstract baseclass however the solution given by Marc here, essentially reduces the multiple inheritance of an abstract class and interface down into a single inheritance design.

The problem for me is, I actually require multiple interface inheritance like this:

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

Here I cannot trivialise this by making TestBase implement ITestTypeA or ITestTypeB (as was the solution for the other question) because the concrete classes TestTypeA should implement both ITestTypeA and ITestBase, and TestTypeB should implement ITestTypeB and ITestBase.

And I'm using protobuf-net v2.0.0.480

Community
  • 1
  • 1
Joe
  • 11,147
  • 7
  • 49
  • 60

1 Answers1

1

I found this workable solution. Not sure if it's recommended or if it'll break unknowingly in runtime but it seems to work for my tests so far.

Because protobuf-net treats a interface like a concrete class for serialization, it hits multiple inheritance issues (that's what i understand of it) so all I do is inherit from one base class and do not specify the relationship between any class to their interfaces.

And create a concrete base class that can be used to define the class hierarchy as below.

[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 
{
}

Actually, all the [ProtoContract] in front of the interfaces may not even matter. I'm finding that taking them out all together seems to work too.

Joe
  • 11,147
  • 7
  • 49
  • 60