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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:06:44+00:00 2026-05-23T19:06:44+00:00

protobuf-net uses nested protobuf constructs to support inheritance. However, can it be made to

  • 0

protobuf-net uses nested protobuf constructs to support inheritance. However, can it be made to push properties into a flat target class that has the same properties as the inherited “serialized” version?

See the test example below. Needless to say the result of the Flat namespace is null for both properties.


Possible solution: copy data into flat.B first on a property by property basis.
Note: this is not the prefered option.


using System;

namespace hierarchy
{
    using ProtoBuf;

    [ProtoContract]
    public class A
    {
        [ProtoMember(1)]
        public string prop1 { get; set; }
    }

    [ProtoContract]
    public class B : A
    {
        public B()
        {
        }

        [ProtoMember(1)]
        public string prop2 { get; set; }

        public override string ToString()
        {
            return "prop1=" + prop1 + ", prop2=" + prop2;
        }

    }
}

namespace flat
{
    using ProtoBuf;

    [ProtoContract]
    public class B
    {
        [ProtoMember(1)]
        public string prop1 { get; set; }

        [ProtoMember(2)]
        public string prop2 { get; set; }

        public override string ToString()
        {
            return "prop1=" + prop1 + ", prop2=" + prop2;
        }
    }
}

namespace TestProtoSerialization
{
    using ProtoBuf;
    using System.IO;

    public class Test2
    {
        public void Test()
        {
            var hb = new hierarchy.B();
            hb.prop1 = "prop1";
            hb.prop2 = "prop2";

            var ms = new MemoryStream();

            Serializer.Serialize<hierarchy.B>(ms, hb);

            var flatB = Serializer.Deserialize<flat.B>(ms);

            Console.WriteLine(hb.ToString());     // <----- Output: prop1=prop1, prop2=prop2  
            Console.WriteLine(flatB.ToString());  // <----- Output: prop1=, prop2=
        }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            var o2 = new Test2();
            o2.Test();
        }
    }

}
  • 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-23T19:06:45+00:00Added an answer on May 23, 2026 at 7:06 pm

    Not directly, and I’m not sure there is a great need to. Maybe I am missing something in the example…

    To pick up on the key point – even forgetting about inheritance you’ve broken the contract – te fields in your exampl are 1 & 1 in one model and 1 & 2 in the other.

    It really depends what your objective is; if you just want to push the data over, then sure you can set up a RuntimeTypeModel that only knows about the derived type (disable automatic configuration and add the fields manually). This will then only work for the derived type (obviously), but will output the data as expected by the flat model:

    var model = TypeModel.Create();
    model.Add(typeof(B), false)
        .Add("prop1", "prop2");
    

    Then use model.Serialize etc.

    However, writing a flat conversion method on c#, or using AutoMapper would be more obvious. I would only use the above if my objective is to remove the inheritance from the output, for example for interoperability reasons.

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

Sidebar

Related Questions

protobuf-net v2 r450 inheritance Hoping someone can help point me in the right direction
I'm trying to talk to a C# program that uses protobuf-net from an iphone
I'm having issues deserializing certain Guid properties of ORM-generated entities using protobuf-net. Here's a
I'm looking for a rpc over tcp implementation that uses protobuf-net (or any other
I am trying to migrate existing code that uses XmlSerializer to protobuf-net due to
I had protobuf.net deserialize invalid (random) bytes into a KeyValuePair (i.e. not nullable). Instead
Can I use net.tcp bindings for protobuf-net WCF? Can I use ClientBase or I
I have serialized a list of objects with protobuf-net. Theoretically, the .bin file can
C#, How can I use ProtoBuf .NET to serialize a dataTable? Can I please
In protobuf-net can i have a byte field? Is a byte array field part

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.