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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:48:28+00:00 2026-06-08T09:48:28+00:00

I’m trying to write serialization code for a class that looks like this: public

  • 0

I’m trying to write serialization code for a class that looks like this:

public class EventMessage
{
    public Dictionary<string, object> Headers { get; set; }
    public object Body { get; set; }
}

The ‘object’ type members in this class hold objects from a quite small (basically, restricted to one assembly + basic CLR types) type set. So I figured i could simply list those classes as subtypes of object class like this:

model.Add(typeof(object), false)
    .AddSubType(1, typeof(X))
    .AddSubType(2, typeof(Y))
    ....

This way I don’t have to embed type information into the serialized message since the deserialization of the object class itself works much like a switch statement – it checks which one of the tags is present in the message and deserializes to appropriate type. I like this, because then I can freely rename classes and move them around assemblies without breaking things.

This worked great in my tests until I started testing headers, which mostly contain string data. I cannot mark string as a subtype of object since I’m getting this exception:

Data of this type has inbuilt behaviour, and cannot be added to a model in this way: System.String

Is there any way I can achieve this behavior or do I have to embed type information inside messages to serialize such class?

  • 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-08T09:48:30+00:00Added an answer on June 8, 2026 at 9:48 am

    protobuf-net stores structured data; object is inherently not structured, and it won’t let you simply declare subclasses for object, nor will it let you monkey with fundamental types such as string, which has very particular rules for serialization.

    This worked great in my tests

    If it did, that’s a bug; there’s no way that should work. At all. It certainly isn’t a supported scenario, and won’t be guaranteed to do the right thing (it could also significantly break things). I will be changing the code to explicitly cause an exception in this case. I’m adding the following, to be fixed ASAP:

    Hmmm…. I though this was an invalid scenario, but my regression tests highlight that there is some “prior art” for this, specifically: Why string interning on serialization in protobuf-net does not work in this example?

    (which is why every time I answer a non-trivial protobuf-net question, I add it as a regression test, so I wasn’t lying)

    I guess I shouldn’t exclude it, but please think of the kittens: that is not my recommended way of doing it. I guess I can’t kill it now, though. Emphasis, though: I cannot enable this for adding string (etc) as a sub-type. Consider that API message-type only, meaning your custom class/structs.

    This way I don’t have to embed type information into the serialized message since the deserialization of the object class itself works much like a switch statement

    protobuf-net doesn’t usually embed type information either ;p Although there is some “dynamic” support that does include type information, but it isn’t needed for this scenario.

    The supported way to do this without embedding any type information would be to encapsulate the values you want, something like:

    [TestFixture]
    public class SO11641262
    {
        [Test]
        public void Execute()
        {
            var model = TypeModel.Create();
            model.Add(typeof (FooData), true)
                .AddSubType(1, typeof (FooData<string>))
                .AddSubType(2, typeof (FooData<int>))
                .AddSubType(3, typeof (FooData<SomeOtherType>));
    
            var val = FooData.Create("abc");
            var clone = (FooData)model.DeepClone(val);
            Assert.AreEqual("abc", clone.ValueUntyped);
            Assert.AreEqual(typeof(string), clone.ItemType);
    
        }
    
        [ProtoContract]
        public abstract class FooData
        {
            public static FooData<T> Create<T>(T value)
            {
                return new FooData<T> {Value = value};
            }
            public abstract Type ItemType { get; }
            public abstract object ValueUntyped { get; set; }
        }
        [ProtoContract]
        public class FooData<T> : FooData
        {
            [ProtoMember(1)]
            public T Value { get; set; }
    
            public override Type ItemType
            {
                get { return typeof (T); }
            }
            public override object ValueUntyped
            {
                get { return Value; }
                set { Value = (T) value; }
            }
        }
        [ProtoContract]
        public class SomeOtherType {}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.