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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:48:08+00:00 2026-05-28T18:48:08+00:00

I am using protobuf-net for performance reasons in a scenario where the assemblies that

  • 0

I am using protobuf-net for performance reasons in a scenario where the assemblies that deserialize saved data are the same that serialized it.

Most of the types that I serialize are simple contracts that I marked with ProtoContract and ProtoMember attributes but occasionally I have to serialize weird objects with many subclasses (ie: Exception).

I made it work with the following workaround using classic ISerializable mechanism.

I am pretty new to protobuf-net and would like to know if this is a good idea and if there are better/standard ways to do it.

My workaround:

I defined a generic surrogate that implements classic serialization

[ProtoContract]
class BinarySerializationSurrogate<T>
{
    [ProtoMember(1)]
    byte[] objectData = null;

    public static implicit operator T(BinarySerializationSurrogate<T> surrogate)
    {
        T ret = default(T);
        if (surrogate == null)
            return ret;

        var serializer = new BinaryFormatter();
        using (var serializedStream = new MemoryStream(surrogate.objectData))
            ret = (T)serializer.Deserialize(serializedStream);

        return ret;
    }

    public static implicit operator BinarySerializationSurrogate<T>(T obj)
    {
        if (obj == null)
            return null;

        var ret = new BinarySerializationSurrogate<T>();

        var serializer = new BinaryFormatter();
        using (var serializedStream = new MemoryStream())
        {
            serializer.Serialize(serializedStream, obj);
            ret.objectData = serializedStream.ToArray();
        }

        return ret;
    }
}

In initialization code I add it as surrogate for weird base types

RuntimeTypeModel.Default
    .Add(typeof(Exception), false)
    .SetSurrogate(typeof(BinarySerializationSurrogate<Exception>));
  • 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-28T18:48:09+00:00Added an answer on May 28, 2026 at 6:48 pm

    That hybrid setup isn’t a scenario that protobuf-net supports directly, and it isn’t something I see as a core use-case, but your surrogate approach should work without any major problems (as long as the assemblies stay in sync).

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

Sidebar

Related Questions

I'm using Protobuf-net. Suppose I have a list of Gizmo objects serialized and that
Using Protobuf-Net, I see that it does not seem possible to deserialize a class
I'm trying to read multiple files that have been serialized with ProtoBuf.NET using .NET
I'm using protobuf-net r278 in C#, and I just noticed that if I have
I have serialized a collection to a file using protobuf-net. I am looking for
I have a WCF service hosted that is returning a byte[] of protobuf-net serialized
using protobuf-net.dll Version 1.0.0.280 When I deserialize a DateTime (wrapped in an object), the
I am using v2 rev 421. When I saved the stream produced by protobuf-net
I started using Protobuf-Net in C# and WPF projects. I have a class that
I'm writing a system that has a set of protocol buffers (using protobuf-net), I

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.