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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:11:13+00:00 2026-06-12T05:11:13+00:00

I translate my project to use protobuf-net instead of BinaryFormatter. It looks like there

  • 0

I translate my project to use protobuf-net instead of BinaryFormatter.
It looks like there is a lack of documentation http://code.google.com/p/protobuf-net/w/list
I also looked up some examples from http://code.google.com/p/protobuf-net/source/browse/
But some things is still not clear for me and that’s why i decide to ask here:

1. About ISerializable and Serializer.Merge/Serialize

If we have inheritance from ISerializable to make specific serialization.
As i read from: ProtoBuf-Net ProtoInclude Generic Type Subclass
we have to use a hook Serializer.Merge/Serialize;

Suppose we have class:

[Serializable]
[ProtoContract]
public class Anchor : ISerializable
{       
    [ProtoMember(1)]
    public int id;

    [ProtoMember(2)]
    public Ship ship;
    ...
 }

the Serializer.Merge(info, this); must be added to constructor Anchor(SerializationInfo info, StreamingContext context)

and Serializer.Serialize(info, this); added to void GetObjectData(SerializationInfo info, StreamingContext context)

So, we have:

    protected Anchor(SerializationInfo info, StreamingContext context)
    {
        //for binaryformatter:            
        Type myType = typeof(Anchor);
        foreach (SerializationEntry e in info)
        {
            FieldInfo f = myType.GetField(e.Name,BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Public|BindingFlags.Instance);
            f.SetValue(this,e.Value);
        }

        //added for protobuf-net:
        Serializer.Merge(info, this);
   }

    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        //for binaryformatter:
        foreach (FieldInfo f in typeof(Anchor).GetFields(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance))
        {
            if ((f.Attributes&FieldAttributes.NotSerialized)==0)
                info.AddValue(f.Name,f.GetValue(this));
        }

       //added for protobuf-net:
        Serializer.Serialize(info, this);
    }

Questions:
Is this correct? (an “info” is overwrited by Serializer? i.e. binaryformatter will not work properly? at the present point in time i just try to use protobuf-net and l would prefer to leave binaryformatter works properly also)

2. About usage ProtoInclude and RuntimeTypeModel.Default

Suppose we have class Message that is base for classes: class Ack, class HandshakeClientInfo…class Command .
If we want to serialize Message’s children, as i read from: protobuf-net's [ProtoInclude(1, "MyClass")] did not work
we should use a ProtoInclude (“to tell” base class: class Message about it’s childrens) in case if we know about children types at compile time – it’s ok.

For those childrens which type we cannot determine at compile time (because it is in another project) we should use RuntimeTypeModel.Default[typeof(Message)].AddSubType(207, typeof(Command));
or to use Type.AssemblyQualifiedName: [ProtoInclude(207, “Trainer.Commands.Command, Simulator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”)]

[Serializable]
[ProtoContract]
[ProtoInclude(200, typeof(Ack))]
[ProtoInclude(201, typeof(HandshakeClientInfo))]
[ProtoInclude(202, typeof(HandshakeReadyToSimulation))]
[ProtoInclude(203, typeof(FileMessage))]
[ProtoInclude(204, typeof(HandshakeResponse))]
[ProtoInclude(205, typeof(Sync))]
[ProtoInclude(206, typeof(HandshakeSimulationStart))]    

public abstract class Message {

    [ProtoMember(1)]
    public byte Sender;
...
}

i use protobuf-net v2 (r580) and the variant with RuntimeTypeModel.Default[typeof(Message)].AddSubType(207, typeof(Command)); seems is more preferable.

Questions:
But i don’t understand where it should be placed in code? in constructor or…. ?
And is it allowable to use ProtoInclude and RuntimeTypeModel.Default together?

  • 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-12T05:11:14+00:00Added an answer on June 12, 2026 at 5:11 am

    First, I need to make very clear the relationship between protobuf-net and ISerializable. Firstly, ISerializable is only used by BinaryFormatter. protobuf-net will never look for this interface, and will never directly use this interface. So, you might ask, why does protobuf-net even mention this?

    Answer: because some people have existing code that is required to use BinaryFormatter, but want to use something else internally. A good example of this might be existing code that uses remoting. Here, protobuf-net can be used to implement ISerializable, essentially replacing the guts of BinaryFormatter. Typical usage would be as follows:

    protected Anchor(SerializationInfo info, StreamingContext context)
    {
        Serializer.Merge(info, this);
    }
    
    public virtual void GetObjectData(SerializationInfo info,
        StreamingContext context)
    {
        Serializer.Serialize(info, this);
    }
    

    This then uses protobuf-net internally; protobuf-net will serialize the object (and any child data), and store the data in a single field; during deserialization it will reverse this. The ISerializable interface is not used to add additional information into protobuf-net. In most cases, if you wanted to separately support protobuf-net and BinaryFormatter, then there would be no Serializer / protobuf-net code in the constructor/GetObjectData, nor would you need to make any changes whatsoever to any existing ISerializable implementation that you have already (or not). That is only for the very specific scenario where you want to change how BinaryFormatter works from the inside. And in that scenario, you would typically hand full control over to protobuf-net.


    For your question on sub-types; yes, you can use [ProtoInclude] in combination with RuntimeTypeModel.Default. Basically, by default (it can be tweaked), the first time protobuf-net sees a type, it will check the attributes. If any are there it will use those attributes to configure the model. However, you can still make any changes you need to the configuration until it has to serialize/deserialize that type, at which point it bakes the configuration information into a strategy for doing the work. Once it has decided on a strategy, it doesn’t like you changing the configuration.

    Therefore, the best time to configure your model is: at app startup. Or at least, before you start doing anything interesting. For completeness, I should note that you can also ask it to ignore the attributes, if you want to configure the model entirely manually (this is pretty rare).

    So your line:

    RuntimeTypeModel.Default[typeof(Message)].AddSubType(207, typeof(Command));
    

    at app startup would be fine.

    In some (rare) cases, you might discover new types much later. For various very complex reasons it isn’t very practical to allow changes to an existing model after you’ve started serializing, but: RuntimeTypeModel.Default is just a default instance. In some more advanced scenarios, what you might want to do is maintain your own model, and then configure a new more knowledgeable one as you need to. So, instead of using RuntimeTypeModel.Default, you might use:

    static RuntimeTypeModel serializer = TypeModel.Create();
    

    then at a later time, you can configure a new setup:

    var workInProgress = TypeModel.Create();
    // TODO: add whatever you need here
    serializer = workInProgress;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am now going to use the .jar file on http://code.google.com/p/google-api-translate-java/ . However, after
I'm correctly use JQuery FullCalendar in a project but I want to translate some
I'd like to use jQuery's auto complete in a Drupal project to automatically find
I was given a block of c++ code that looks like it was from
In my Ruby on Rails Project I use HAML, I need to translate something
I have the following project solution: There is a ASP.NET MVC Web Application where
I am trying to use the Google Translate v2 api in my app engine
I am currently working on a private project that is going to use Google's
I'm doing i18n for a php project using gettext. I'd like to use the
I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but

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.