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

  • Home
  • SEARCH
  • 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 1086291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:47:41+00:00 2026-05-16T22:47:41+00:00

I am prototyping Protobuf-net to replace some of our existing C# code which is

  • 0

I am prototyping Protobuf-net to replace some of our existing C# code which is currently using [Datacontract] to Serialize objects to Xml.

Using protobuffer we can easily share data with Java. I am thus very interested in the .proto file generation of Protobuf-net. This worked well for almost all the use cases I tested so far.

But now with inheritance it’s a different ball game. The .proto file that’s been generated for the Inherited classes is very simple – not including any of the fields for the Base Class.

The inheritance itself is working fine in C# – I can read the generated byte stream (see my test below). So the internal binary stream contains all the fields of the base class

The generated .proto:

message ProtoScholar {
   optional string Subject = 1;
}

If I can understand how the byte stream is written out I can create the corresponding .proto file by hand.

Anyone got any experience in creating a .proto file for inheritance using protobuf-net?

Any info on how the data stream is being created for inheritance will be great.

My DataModel is as follows:

[DataContract]
[ProtoInclude(7, typeof(ProtoScholar))]
public class ProtoAlien
{

    [DataMember(Order = 1)]
    public string Name
    {
        get;
        set;
    }


    [DataMember(Order = 2)]
    public double? Average
    {
        get;
        set;
    }

    [DataMember(Order = 3)]
    public int? HowMuch
    {
        get;
        set;
    }

    [DataMember(Order = 4, IsRequired = true)]
    public Height Length
    {
        get; set;
    }


    [DataMember(Order = 5, IsRequired = true)]       
    public Character Personality
    {
        get;
        set;
    }

    [DataMember(Order = 6, IsRequired = true)]       
    public DateTime When
    {
        get; set;
    }

    public enum Height
    {
        Short = 1,
        Medium,
        Tall
    }

    public enum Character : long
    {
        Wasp = 1717986918,
        BumbleBee,
        WorkerBee,
        Hornet,
        Queen
    }        
}

[DataContract()]
public class ProtoScholar : ProtoAlien
{

    [DataMember(Order=1)]
    public string Subject
    {
        get; set;
    }

}

My NUnit test looks like this:

[Test]
    public void TestInheritanceSupport()
    {

        var protoBuffer = new ProtoScholar
        {
            Name = "Ben11",
            HowMuch = null,
            Length = ProtoAlien.Height.Tall,
            Personality = ProtoAlien.Character.WorkerBee,
            When = new DateTime(2010, 4, 1, 2, 33, 56, 392),
            Subject = "Alien Economics"
        };


        using (var ms = new MemoryStream())
        {

            var proto = Serializer.GetProto<ProtoScholar>();
            Console.WriteLine(proto);

            //Serialize to a Memory Stream
            Serializer.Serialize(ms, protoBuffer);

            Console.WriteLine(ms.Length);
            ms.Position = 0;
            var deserializedProtoBuffer = Serializer.Deserialize<ProtoScholar>(ms);

            Assert.AreEqual("Ben11", deserializedProtoBuffer.Name);

            Assert.Null(deserializedProtoBuffer.HowMuch);
            Assert.AreEqual(ProtoAlien.Height.Tall, deserializedProtoBuffer.Length);

            Assert.AreEqual(ProtoAlien.Character.WorkerBee, deserializedProtoBuffer.Personality);
            Assert.AreEqual(new DateTime(2010, 4, 1, 2, 33, 56, 392), deserializedProtoBuffer.When);
            Assert.AreEqual("Alien Economics", deserializedProtoBuffer.Subject);

        }

    }
  • 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-05-16T22:47:42+00:00Added an answer on May 16, 2026 at 10:47 pm

    Since inheritance is not part of the core spec, I basically represent this using encapsulation. So your [ProtoInclude] maps to:

    message ProtoAlien {
       // other fields 1 thru 6 [snip]
       optional ProtoScholar ProtoScholar = 7;
    }
    message ProtoScholar {
       optional string Subject = 1;
    }
    

    GetProto<T>() is undergoing an overhaul in v2, so it should support a few more of these scenarios.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've used Yahoo Pipes for some prototyping, but I'm unwilling reluctant to use it
I am prototyping a Django web-app that will provide a static media files (PDFs,
I want rapid prototyping: Quickly define screens, links, buttons etc. I want to easily
I'm designing a web application - prototyping and wireframing the main pages so I've
I would like to hack around the Android default Settings app for prototyping purposes.
The question says it all, we know most of the hosted web-based UI prototyping
I'm interested in doing rapid app development in Python. Since this is mainly for
I've run into a problem where I cannot stop mnesia within my program without
I am working on the prototype for a scheduling application on an intranet system.

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.