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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:09:56+00:00 2026-05-23T12:09:56+00:00

I have two classes that I would like to transform to protobuf messages: [ProtoContract]

  • 0

I have two classes that I would like to transform to protobuf messages:

[ProtoContract]
class ClassA {
    [ProtoMember(1)]
    public int b;
    [ProtoMember(2)]
    public int c;
}

[ProtoContract]
class ClassD : ClassA
{
    [ProtoMember(3)]
    public int e;
    [ProtoMember(4)]
    public int f;
}

What I am trying to achieve is a serialization of ClassA containing b and c. And when serializing ClassD i will get b, c, e and f.

Is this possible, or what kind of approach should I use? I’m trying to avoid nested messages.

Using my approach I get a problem when serializing a ClassD object.

ClassD d = new ClassD();
Serialize.Serialize<ClassA>(stream, d);
Serialize.Serialize<ClassD>(stream, d);

In both attempts above the serialized data only contain the properties in ClassD and none from ClassA.
I would expect at least none of the properties in ClassD to be serialized in the first case
and I would like all, both from ClassA and ClassD to be serialized in the second case.

How would I approach this problem?

  • 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-23T12:09:57+00:00Added an answer on May 23, 2026 at 12:09 pm

    It looks like this works in v2 without inheritance:

        var model = TypeModel.Create();
        model.Add(typeof(ClassA), false).Add("b", "c");
        model.Add(typeof(ClassD), false).Add("b", "c", "e", "f");
    
        var a = new ClassA { b = 1, c = 2 };
        var aClone = (ClassA)model.DeepClone(a);
        Debug.Assert(aClone.b == 1);
        Debug.Assert(aClone.c == 2);
    
        var d = new ClassD { b = 1, c = 2, e = 3, f = 4};
        var dClone = (ClassD)model.DeepClone(d);
        Debug.Assert(dClone.b == 1);
        Debug.Assert(dClone.c == 2);
        Debug.Assert(dClone.e == 3);
        Debug.Assert(dClone.f == 4);
    

    These will each be flat models, not nested. At the moment this would need explicit setup (as above), as it isn’t part of how it builds the model by default – but as a corollary you don’t need attributes here:

    class ClassA
    {
        public int b;
        public int c;
    }
    class ClassD : ClassA
    {
        public int e;
        public int f;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes that I would like to specify as follows: class Club
I have two classes that I would like to merge into a composite. These
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes orchestrated by a main class and I would like to
I have two classes, ThreadItem and Enquiry. public class ThreadItem { [Key] public int
Suppose I have two classes: public class Student { public int Id {get; set;}
I have two classes, Phone.class and Time.class. I would like to use their methods
Let's say I have two classes like the following: Class A { public: ..
Say I have two traits that I would like to mixin to a class.
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID

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.