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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:33:23+00:00 2026-05-23T03:33:23+00:00

In a project, I’m currently using a DataContractSerializer to serialize my data. And I’d

  • 0

In a project, I’m currently using a DataContractSerializer to serialize my data. And I’d like to use Protobuf instead for performance reason.
I use a serializer to store blobs inside a database, and I want to keep the data versioning features of WCF to handle more easily data contract modifications once the project is released in the real world.

The data contracts of these blobs are not known at compile time. (A list of “extension” is specified in the config file and so the extensions register DataContracts they know at runtime)

However it seems that the only way to tell protobuf about known type is with the [ProtoInclude] attribute.

To be more specific, here is an example of what I want to serialize.

[DataContract]
public class Mydata
{
    [DataMember(Order = 1)]
    public int Saved
    {
        get;
        set;
    }
}

[DataContract]
public class Container
{
    [DataMember(Order = 1)]
    public object Data
    {
        get;
        set;
    }
}

With DataContractSerializer here how I do :

[TestMethod]
public void SerializeWithDataContractSerializer()
{
    var container = new Container()
    {
        Data = new Mydata()
        {
            Saved = 1
        }
    };
    DataContractSerializer serializer = new DataContractSerializer(typeof(Container), new[] { typeof(Mydata) });
    var ms = new MemoryStream();
    serializer.WriteObject(ms, container);
    ms.Position = 0;
    var containerSerialized = (Container)serializer.ReadObject(ms);
    Assert.AreEqual(((Mydata)container.Data).Saved, ((Mydata)containerSerialized.Data).Saved);
}

With protobuf here how I wanted to do (but not possible Serializer.CreateFormatter<T>() does not take a knownTypes parameter) :

[TestMethod]
public void SerializeWithProtoBuf()
{
    var container = new Container()
    {
        Data = new Mydata()
        {
            Saved = 1
        }
    };

    var formatter = Serializer.CreateFormatter<Container>(new[] { typeof(Mydata) });
    var ms = new MemoryStream();
    formatter.Serialize(ms, container);
    ms.Position = 0;
    var containerSerialized = (Container)formatter.Deserialize(ms);
    Assert.AreEqual(((Mydata)container.Data).Saved, ((Mydata)containerSerialized.Data).Saved);
}

Any solution ?

  • 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-23T03:33:24+00:00Added an answer on May 23, 2026 at 3:33 am

    Since you are serialising a root object, in v1 (the fully released dll) you can do this using the Serializer.NonGeneric.SerializeWithLengthPrefix and DeserializeWithLengthPrefix API, in particular using a different field-number (tag) per-type when serialising, and using the deserialize overload that accepts a type-resolver to translate a irks-number back into a Type. I’m not at a PC currently but I can add an example later of you need.

    In “v2” there are additional options here:

    • you can mark a property (etc) as DynamicTypr=true, which stores additional type metadata making this work even for object properties, but making it a bit more tied to your model
    • you can add known sub-types at runtime via the flexible type-model; however generally this wouldn’t be from object, and I am still exploring possible side-effects of this (it has been requested as a feature previously)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My project is currently using a svn repository which gains several hundred new revisions
The project I'm working is using n-tier architecture. Our layers are as follows: Data
Project Scenario Technology : Dotnetnuke (Approx. 100 - 150 screens) Data Architecture : LINQ
Project#1 has some interfaces and classes that project#2 references. Now I want to use
project: Using VB.NET to build a winforms database interface and work-automation app. I am
project: Using VB.NET to build a winforms database interface and work-automation app. I am
Our project uses XJC to generate Java classes from an XSD. I'm using JAVA
My project contains tagcloud and I am following the sample on: http://www.mikesdotnetting.com/Article/107/Creating-a-Tag-Cloud-using-ASP.NET-MVC-and-the-Entity-Framework My problem
I created a project without using the google app engine (by unchecking the checkbox
Project Darkstar was the topic of the monthly JavaSIG meeting down at the Google

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.