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 8590495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:18:25+00:00 2026-06-11T23:18:25+00:00

I have read the various posts to do with inheritance and that Protocol Buffers

  • 0

I have read the various posts to do with inheritance and that Protocol Buffers does not support inheritance. I don’t want inheritance in the Protocol Buffers messages but rather inheritance so I can deal with all my Protocol Buffers messages easily.

I am using protobuf-net 2.0.0.480 and a .proto file to define my protocol. It all works well except when I get to the point where I would like a common ancestor so that I can do some common functionality and allow for easy inspection. A simple example:

My .proto file:

message ProtocolInformation {
  enum MessageKinds {
    LAYOUT_ADVANCE = 1;
    LAYOUT_RENDER = 2;                             
  }
  required MessageKinds MessageKind = 1;  
  required int32 UniqueID = 2;            
} 

message GFX_Layout_Advance {
  required ProtocolInformation ProtocolInfo = 1;
  required int32 LayoutHandle = 2;
}

message GFX_Layout_Render {
  required ProtocolInformation ProtocolInfo = 1;
  required int32 LayoutHandle = 2;
  required int32 Stage = 3;
}  

which ultimately generates classes for GFX_Layout_Advance, GFX_Layout_Render as (only a portion of GFX_Layout_Advance) shown:

[global::System.Serializable, global::ProtoBuf.ProtoContract(Name = @"GFX_Layout_Advance")]
public partial class GFX_Layout_Advance : global::ProtoBuf.IExtensible
{
    public GFX_Layout_Advance() { }

    private GFX_Protocol.ProtocolInformation _ProtocolInfo;
    [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name = @"ProtocolInfo", DataFormat = global::ProtoBuf.DataFormat.Default)]
    public GFX_Protocol.ProtocolInformation ProtocolInfo 

As it was a partial class and there appeared to be no overrideable constructor I implemented:

public partial class GFX_Layout_Advance : GfxProtocolMessageBase
    {
        public override ProtocolInformation ProtocolInformation()
        {
            return ProtocolInfo;
        }
    } 

This would allow me to treat all incoming messages as GfxProtocolMessageBase and allow querying of ProtocolInformation so that I can cast to the appropriate descendant. In this case GFX_Layout_Advance. However…..

  • Adding the additional partial class GFX_Layout_Advance() causes a different protobuf encoding. As the only change to the interface is a method I don’t understand why this is?

Bottom line is:

  1. I want to introduce a common base ancestor to all the generated protobuf-net classes
  2. Base ancestor class will allow me access to information about what kind of message I am dealing with as I don’t want to have to cast to the actual message type until I am ready

How do I achieve 1. & 2.?

All pointers appreciated.

  • 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-11T23:18:26+00:00Added an answer on June 11, 2026 at 11:18 pm
    1. Yes that should work fine as long as GfxProtocolMessageBase is not a contract type. It uses partial classes deliberately to allow this type of thing. The encoded data should not change. If you have a scenario that misbehaves that I can look at I will happily investigate.

    2. That’s fine; just: don’t use Serializer.Serialize<GfxProtocolMessageBase> / Serializer.Deserialize<GfxProtocolMessageBase>, as the serializer should not know about GfxProtocolMessageBase (unless of course you’re happy to, but that does mean you won’t be following the existing .proto 100%). For serialization, either Serializer.NonGeneric.Serialize or typeModel.Serialize (for example, RuntimeTypeModel.Default.Serialize) will do the right thing automatically. For deserialization, you will need to know the actual target Type.

    Of course, the alternative option is to allow GfxProtocolMessageBase to be known to the serializer as a base-type, and use protobuf-net’s inbuilt inheritance support ([ProtoInclude(...)] etc) – but the problem then is: that won’t map 100% to your .proto, as inheritance is implemented (by protobuf-net) as encapsulation, meaning: it will be written as though it is a base-message with a number of optional sub-message fields.


    Edit to show type-resolver usage for reading different objects (of heterogeneous types) from a single stream:

    using ProtoBuf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    [ProtoContract]
    class Foo
    {
        [ProtoMember(1)]
        public int Id { get; set; }
    
        public override string ToString()
        {
            return "Foo with Id=" + Id;
        }
    }
    [ProtoContract]
    class Bar
    {
        [ProtoMember(2)]
        public string Name { get; set; }
    
        public override string ToString()
        {
            return "Bar with Name=" + Name;
        }
    }
    static class Program
    {
        // mechanism to obtain a Type from a numeric key
        static readonly Dictionary<int, Type> typeMap = new Dictionary<int, Type>
        {
            {1,typeof(Foo)}, {2,typeof(Bar)}
        };
        static Type ResolveType(int key)
        {
            Type type;
            typeMap.TryGetValue(key, out type);
            return type;
        }
        static void Main()
        {
            // using MemoryStream purely for convenience
            using (var ms = new MemoryStream())
            {
                // serialize some random data (here I'm coding the outbound key
                // directly, but this could be automated)
                Serializer.SerializeWithLengthPrefix(ms, new Foo { Id = 123 },
                    PrefixStyle.Base128, 1);
                Serializer.SerializeWithLengthPrefix(ms, new Bar { Name = "abc" },
                    PrefixStyle.Base128, 2);
                Serializer.SerializeWithLengthPrefix(ms, new Foo { Id = 456 },
                    PrefixStyle.Base128, 1);
                Serializer.SerializeWithLengthPrefix(ms, new Bar { Name = "def" },
                    PrefixStyle.Base128, 2);
    
                // rewind (this wouldn't be necessary for a NetworkStream,
                // FileStream, etc)
                ms.Position = 0;
    
                // walk forwards through the top-level data
                object obj;
                while (Serializer.NonGeneric.TryDeserializeWithLengthPrefix(
                    ms, PrefixStyle.Base128, ResolveType, out obj))
                {
                    // note we overrode the ToString on each object to make
                    // this bit work
                    Console.WriteLine(obj);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read through the various posts on SO about this, but have still not
I am creating a tag cloud and have read the various posts that describe
I have read various posts in stackoverflow and I am trying to figure out
I have read in various places that having variables with global scope, i.e. a
I read through all existing posts on the same topic, but I still have
I read have most if not all of the various articles on adding MVC
I've read in various places that one important requirement in DDD is to have
I've read in multiple posts on SO that if users have Javascript disabled, ideally
I have read various posts here at StackOverflow regarding the execution of FFT on
I have read various articles suggesting to use Prepared statements in SQL queries, but

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.