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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:36:48+00:00 2026-06-17T00:36:48+00:00

I have two simple classes which I want to serialize with protobuf-net. Everything looks

  • 0

I have two simple classes which I want to serialize with protobuf-net. Everything looks OK but when I deserialize the Body property is null. Any idea what I am doing wrong?

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using ProtoBuf;

namespace Protobuf_test
{
    public class Program
    {
        private static void Main(string[] args)
        {
            var innerObject = new Inner() { Id = Guid.NewGuid() };
            var outerObject = new Outer() { Body = innerObject };

            using (var stream = File.Create("serialized.bin"))
            {
                Serializer.Serialize(stream, outerObject);
            }
            using (var file = File.OpenRead("serialized.bin"))
            {
                var deserialized = Serializer.Deserialize<Outer>(file);
            }
        }
    }

    [DataContract]
    public class Inner
    {
        [DataMember]
        public Guid Id { get; set; }
    }

    [DataContract]
    public class Outer
    {
        [DataMember]
        public object Body { get; set; }
    }
}
  • 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-17T00:36:49+00:00Added an answer on June 17, 2026 at 12:36 am

    Two issues:

    1. protobuf-net requires a positive integer key per member; this can be provided via Order= on [DataMember]. Low numbers are better, so typically this means 1, 2, 3…

    2. the protobuf specification is a schema-bound serializer; it wants to understand the data in advance; object does not work nicely.

    but this would work:

    [DataContract]
    public class Inner
    {
        [DataMember(Order=1)]
        public Guid Id { get; set; }
    }
    
    [DataContract]
    public class Outer
    {
        [DataMember(Order=1)]
        public Inner Body { get; set; }
    }
    

    There is some support for flexibly typed data (i.e. object), but note that it is protobuf-net specific (it will not play nicely re portability). I would encourage you to look at the typed approach first. But it can do that: I’d just prefer it if you didn’t ;p

    If you cannot change the existing model, then the first thing I would advise would be: add a separate DTO model that looks a lot like your existing model, but marked for serialization, and work with that – and shim between the two models. If that isn’t an option, you’ll need to configure the system, to a: define keys for members, and b: tell it to use dynamic. For example:

    var config = RuntimeTypeModel.Default;
    config.Add(typeof(Inner),false).Add("Id");
    config.Add(typeof(Outer), false).AddField(1, "Body").DynamicType = true;
    

    (note: Serializer.* is basically a short-cut to RuntimeTypeModel.Default.*, so this configuration does talk to methods on Serializer.*)

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

Sidebar

Related Questions

I have two simple POCO classes; I'm trying to get the MyY property below
I have a simple Fluent NHibernate model with two related classes: public class Applicant
I have two simple submit buttons on my form, which work just fine: <button
I have simple ontology called campus.owl.There is a class calledLecturer and which has two
A simple question... I have an abstract class Cell and two classes BorderCell and
I have two closely related classes which I'll call Widget and Sprocket. Sprocket has
I have two simple classes public class Blog { public Blog(){ Comments=new List<Comment>(); }
Given 3 classes, A, and B which each have an ID property, and then
Let's say I have two simple models project t.string :title vote t.references :project t.integer
I have two very simple routes routes.MapRoute( post, // Route name postPage + /{slug},

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.