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

  • Home
  • SEARCH
  • 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 236155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:19:21+00:00 2026-05-11T20:19:21+00:00

I have a List<object> with different types of objects in it like integers, strings,

  • 0

I have a List<object> with different types of objects in it like integers, strings, and custom types. All custom types are protobuf-adjusted.
What I wanna do now is to serialize / deserialize this list with protobuf.net. Up until now I suspect that I have to declare each and every type explicitly, which is unfortunately not possible with these mixed-list constructs. Because the binary formater has no problems to do these things I hope that I missed something and that you can help me out.
So my question is how to deal with objects in protobuf.net.

  • 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-11T20:19:21+00:00Added an answer on May 11, 2026 at 8:19 pm

    (disclosure: I’m the author of protobuf-net)

    BinaryFormatter is a metadata-based serializer; i.e. it sends .NET type information about every object serialized. protobuf-net is a contract-based serializer (the binary equivalent of XmlSerializer / DataContractSerializer, which will also reject this).

    There is no current mechanism for transporting arbitrary objects, since the other end will have no way of knowing what you are sending; however, if you have a known set of different object types you want to send, there may be options. There is also work in the pipeline to allow runtime-extensible schemas (rather than just attributes, which are fixed at build) – but this is far from complete.


    This isn’t ideal, but it works… it should be easier when I’ve completed the work to support runtime schemas:

    using System;
    using System.Collections.Generic;
    using ProtoBuf;
    [ProtoContract]
    [ProtoInclude(10, typeof(DataItem<int>))]
    [ProtoInclude(11, typeof(DataItem<string>))]
    [ProtoInclude(12, typeof(DataItem<DateTime>))]
    [ProtoInclude(13, typeof(DataItem<Foo>))]
    abstract class DataItem {
        public static DataItem<T> Create<T>(T value) {
            return new DataItem<T>(value);
        }
        public object Value {
            get { return ValueImpl; }
            set { ValueImpl = value; }
        }
        protected abstract object ValueImpl {get;set;}
        protected DataItem() { }
    }
    [ProtoContract]
    sealed class DataItem<T> : DataItem {
        public DataItem() { }
        public DataItem(T value) { Value = value; }
        [ProtoMember(1)]
        public new T Value { get; set; }
        protected override object ValueImpl {
            get { return Value; }
            set { Value = (T)value; }
        }
    }
    [ProtoContract]
    public class Foo {
        [ProtoMember(1)]
        public string Bar { get; set; }
        public override string ToString() {
            return "Foo with Bar=" + Bar;
        }
    }
    static class Program {
        static void Main() {
            var items = new List<DataItem>();
            items.Add(DataItem.Create(12345));
            items.Add(DataItem.Create(DateTime.Today));
            items.Add(DataItem.Create("abcde"));
            items.Add(DataItem.Create(new Foo { Bar = "Marc" }));
            items.Add(DataItem.Create(67890));
    
            // serialize and deserialize
            var clone = Serializer.DeepClone(items);
            foreach (DataItem item in clone) {
                Console.WriteLine(item.Value);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a List object and an iterator for that list. Now
I have an object, and I want to list all the selectors to which
I have a List object being accessed by multiple threads. There is mostly one
I have a List of a complex type - an object with a few
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have a Request object which contains a list of Approvers. An approver has
I have a field object and I create a list of fields: class Field
I have a Foo object, and a std::list holding instances of it. My problem
I have two iterators, a list and an itertools.count object (i.e. an infinite value
I have a list of objects I wish to sort based on a field

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.