Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:11:55+00:00 2026-06-09T17:11:55+00:00

I get that protobuf do not support inheritance, and since I am using protoc-c

  • 0

I get that protobuf do not support inheritance, and since I am using protoc-c on another end, I do not want to use any extensions as well. However, I am stuck with a C# model that depends on inheritance:

class Header {
     public int version { get; set; }
}

class Message : Header {
     public String message { get; set; }
}

I attempted to switch the inheritance into encapsulation in the wire format to something like this:

[ProtoContract]
class Header {
     [ProtoMember(1)]
     public int version { get; set; }
}

[ProtoContract]
class Message : Header {
     [ProtoMember(1)]
     public Header Header { get { return this; } set { } }

     [ProtoMember(2)]
     public String Message { get; set; }
}

Then I get the “Unexpected sub-type” error which prompts me to:
Why I have to use [ProtoInclude]?

I feel that my case is different than the case in the above question, so would like to ask again for my specific case, where I have tried to inside out the inheritance, is this impossible to do without having ProtoInclude?

If not, how would I do it in v2?

—– EDITED ——

My proto file in the C (using protobuf-c) side is something like this:

message Header {
    optional int32 version = 1;
}

message Message {
    optional Header header = 1;
    optional string message = 2;
}

I do not want to put the Message inside the Header, and I do not want the inheritance-over-the-wire feature. This format enables me to add stuffs into the Header message easily without needing to change the Message message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:11:56+00:00Added an answer on June 9, 2026 at 5:11 pm

    With the edit: no, that scenario is not directly supported – protobuf-net is highly aware of inheritance, and isn’t very amenable to ignoring it. This seems such an unusual case that I’m not desperate to add it, and I thing the return this; getter and no-op setter would cause additional downstream complications (not bugs, since it isn’t expected to support that) which could be pretty hard to rectify.

    I would advise using a model that is similar to the structure you want to represent. If this isn’t directly possible, you can use a surrogate instead. The following works and retains both your intended wire-structure and the existing type inheritance:

    using ProtoBuf;
    using ProtoBuf.Meta;
    
    // DTO model - maps directly to the wire layout
    [ProtoContract]
    class HeaderDto
    {
        [ProtoMember(1)]
        public int Version { get; set; }
    }
    
    [ProtoContract]
    class MessageDto
    {
        [ProtoMember(1)]
        public HeaderDto Header { get { return header;}}
        private readonly HeaderDto header = new HeaderDto();
    
        [ProtoMember(2)]
        public string Message { get; set; }
    
        // the operators (implicit or explicit) are used to map between the
        // primary type (Message) and the surrogate type (MessageDto)
        public static implicit operator Message(MessageDto value)
        {
            return value == null ? null : new Message {
                version = value.Header.Version, message = value.Message };
        }
        public static implicit operator MessageDto(Message value)
        {
            return value == null ? null : new MessageDto {
                Message = value.message, Header = { Version = value.version } };
        }
    }
    
    // domain model
    class Header
    {
        public int version { get; set; }
    }
    
    class Message : Header
    {
        public string message { get; set; }
    }
    
    // example
    static class Program
    {
        static void Main()
        {
            // configure the surrogate
            RuntimeTypeModel.Default.Add(typeof(Message), false)
                            .SetSurrogate(typeof(MessageDto));
            Message msg = new Message { version = 1, message = "abc" };
            var obj = Serializer.DeepClone(msg);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a c++ code that is using a protobuf cc
When using the generated Python code from our protobuf classes, we get this error:
protobuf-net uses nested protobuf constructs to support inheritance. However, can it be made to
How can I get v2 of protobuf-net to ignore the fact that my class
I'm writing a system that has a set of protocol buffers (using protobuf-net), I
I have a class that I serialize, then deserialize using Protobuf-net version r431 (probably
Is anyone aware of any issues when using ProtoBuf-Net to serialize/deserialize between compact framework
I get that the (nolock) optimizer hint allows for dirty reads, but under what
I get that asterix is for pointers, and I understand about pointers now. But
I get that error with the following code. anyone know why? function validateAdminInput(){ var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.