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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:58:23+00:00 2026-06-08T00:58:23+00:00

Does anyone have a working example where protobuf-net can be used as a drop-in

  • 0

Does anyone have a working example where protobuf-net can be used as a drop-in replacement for BinaryFormatter? Is that even possible?

Actually I just need a serializer for one message type which looks like

public class A {
  public B[] Bs { get;set; }
  public C[] Cs { get;set; }
}

All types are defined in a different assembly and have a lot of properties.
Is there an option to automatically generate proto contracts with all public properties included, for class A and other used types (B, C), so something like

var formatter = ProtoBuf.Serializer.CreateFormatter<A>()

just works?

  • 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-08T00:58:25+00:00Added an answer on June 8, 2026 at 12:58 am

    Firstly, protobuf-net is not intended to be, and does not claim to be, a 100% drop in replacement for BinaryFormatter. It has slightly different features.

    Would you be content to do a little reflection? Basically, there is support for ImplicitFields, but currently this is only available via [ProtoContract], and cannot be done conveniently via RuntimeTypeModel, which is a bit of a pain, and is on my list. Although, I should point out that I consider implicit-fields to be a bit risky, and it should only be done if you know the DTO innards won’t change! But: to answer your question, you could iterate over the types you expect, and add them to the model manually:

    static void Main()
    {
        Prepare(typeof(A), typeof(B), typeof(C));
        // if you really want to use IFormatter...
        var formatter = RuntimeTypeModel.Default.CreateFormatter(typeof (A));
        var obj = new A {Bs = new B[] {new B()}};
        using (var ms = new MemoryStream())
        {
            formatter.Serialize(ms, obj);
            ms.Position = 0;
            var clone = formatter.Deserialize(ms);
        }
    }
    static void Prepare(params Type[] types)
    {
        if(types != null) foreach(var type in types) Prepare(type);
    }
    static void Prepare(Type type)
    {
        if(type != null && !RuntimeTypeModel.Default.IsDefined(type))
        {
            Debug.WriteLine("Preparing: " + type.FullName);
            // note this has no defined sort, so invent one
            var props = type.GetProperties(); 
            Array.Sort(props, (x, y) => string.Compare(
                x.Name, y.Name, StringComparison.Ordinal));
            var meta = RuntimeTypeModel.Default.Add(type, false);
            int fieldNum = 1;
            for(int i = 0 ; i < props.Length ; i++)
            {
                meta.Add(fieldNum++, props[i].Name);
            }
    
        }
    }
    

    Note that the use of IFormatter here is entirely unnecessary; you could also use RuntimeTypeModel.Default.Serialize(...) or Serializer.Serialize<T>(...).

    As a footnote: I would advise defining the models more … repeatably. For example:

    RuntimeTypeModel.Default.Add(typeof(A)).Add("Bs", "Cs");
    RuntimeTypeModel.Default.Add(typeof(B)).Add("Foo");
    RuntimeTypeModel.Default.Add(typeof(C)).Add("Bar", "Blap", "Blop");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a working example of a video player built using Qt phonon?
Does anyone have a working example of OAuth authentication for twitter with Android? I
Does anyone have a working class or function to create the hashed email that
Does anyone have a working example of a Wordpress menu outputted manually? The one
ref: Dynamic Control ID Does anyone have a working example of creating the ID
Does anyone have a working example of importing a local file as a module
Does anyone have a working example of using the new GWT constructs for RequestFactory
Does anyone out there have a working example of the Facebook live stream plugin
Does anyone have an example AUTORUN.INF which can launch an MSI installer automatically when
does anyone have a generic working example of the use of the ExpertR9.dll library

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.