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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:02:14+00:00 2026-05-22T16:02:14+00:00

Observe the following code (taken from this question ): [ProtoContract] public class B {

  • 0

Observe the following code (taken from this question):

  [ProtoContract]
  public class B
  {
    [ProtoMember(1)] public int Y;
  }

  [ProtoContract]
  public class C
  {
    [ProtoMember(1)] public int Y;
  }

  class Program
  {
    static void Main()
    {
      object b = new B { Y = 2 };
      object c = new C { Y = 4 };
      using (var ms = new MemoryStream())
      {
        Serializer.SerializeWithLengthPrefix(ms, b, PrefixStyle.Base128);
        Serializer.SerializeWithLengthPrefix(ms, c, PrefixStyle.Base128);
        ms.Position = 0;
        var b2 = Serializer.DeserializeWithLengthPrefix<B>(ms, PrefixStyle.Base128);
        Debug.Assert(((B)b).Y == b2.Y);
        var c2 = Serializer.DeserializeWithLengthPrefix<C>(ms, PrefixStyle.Base128);
        Debug.Assert(((C)c).Y == c2.Y);
      }
    }
  }

Obviously, the code is wrong, because b and c are declared as object, but I serialize them using the generic Serializer.Serialize<T> method:

System.ArgumentOutOfRangeException occurred
  Message=Specified argument was out of the range of valid values.
Parameter name: index
  Source=protobuf-net
  ParamName=index
  StackTrace:
       at ProtoBuf.Meta.BasicList.Node.get_Item(Int32 index)
  InnerException: 

Everything works fine if I redeclare b as B and c as C. However, I need them to be declared as object, so I guess I have to serialize them using the non generic method Serializer.NonGeneric.SerializeWithLengthPrefix, the problem I do not understand the meaning of the extra fieldNumber argument expected by the method. Can someone explain what is it and how should I use it here?

I use protobuf-net v2.

Thanks.

EDIT

I have managed to make it work with the addition of the following code:

RuntimeTypeModel.Default.Add(typeof(object), false).AddSubType(1, typeof(B)).AddSubType(2, typeof(C));

Although it works, the problem is that I need to know at compile time the types used in the serialization (B = 1, C = 2), which is bad for me. Is there a better way?

EDIT2

OK, I have changed the code like so:

public class GenericSerializationHelper<T> : IGenericSerializationHelper
{
  void IGenericSerializationHelper.SerializeWithLengthPrefix(Stream stream, object obj, PrefixStyle prefixStyle)
  {
    Serializer.SerializeWithLengthPrefix(stream, (T)obj, prefixStyle);
  }
}

public interface IGenericSerializationHelper
{
  void SerializeWithLengthPrefix(Stream stream, object obj, PrefixStyle prefixStyle);
}

...

static void Main()
{
  var typeMap = new Dictionary<Type, IGenericSerializationHelper>();
  typeMap[typeof(B)] = new GenericSerializationHelper<B>();
  typeMap[typeof(C)] = new GenericSerializationHelper<C>();

  object b = new B { Y = 2 };
  object c = new C { Y = 4 };
  using (var ms = new MemoryStream())
  {
    typeMap[b.GetType()].SerializeWithLengthPrefix(ms, b, PrefixStyle.Base128);
    typeMap[c.GetType()].SerializeWithLengthPrefix(ms, c, PrefixStyle.Base128);
    ms.Position = 0;
    var b2 = Serializer.DeserializeWithLengthPrefix<B>(ms, PrefixStyle.Base128);
    Debug.Assert(((B)b).Y == b2.Y);
    var c2 = Serializer.DeserializeWithLengthPrefix<C>(ms, PrefixStyle.Base128);
    Debug.Assert(((C)c).Y == c2.Y);
  }
}

Now, in order to serialize the objects I do not need any compile time mapping, I just jump to the respective generic method. Of course, I understand that in this scheme I must know the types at deserialization, which is still a bummer.

  • 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-22T16:02:15+00:00Added an answer on May 22, 2026 at 4:02 pm

    If you are serializing homogeneous data, the field-number is largely irrelevant, with the minor caveat that keeping it as 1 (aka Serializer.ListItemTag) it is trivial to read it back as a list if you want (but pretty easy either way).

    In the case of heterogeneous data, as in this example – there is a non-generic API designed for this purpose (in fact, in v2 all the APIs are non-generic – the generic API simply forwards to the non-generic). By passing in a TypeResolver, you can tell it on the fly how to interpret any tag encountered (at the root of the stream). If you choose to return null for a given tag, it assumes you aren’t interested in that object and skips it entirely (in the example below it will just blow up, obviously – but that is just because the example code is minimal):

    // I'm giving the example in terms of the v2 API, because there is a bug in the 
    // Serializer.NonGeneric code in the beta - simply, in the first beta cut this
    // doesn't correctly forward to the type-model. This will be fixed ASAP.
    TypeModel model = RuntimeTypeModel.Default;
    using (var ms = new MemoryStream())
    {
        var tagToType = new Dictionary<int, Type>
        {  // somewhere, we need a registry of what field maps to what Type
            {1, typeof(B)}, {2, typeof(C)}
        };
        var typeToTag = tagToType.ToDictionary(pair => pair.Value, pair => pair.Key);
    
        object b = new B { Y = 2 };
        object c = new C { Y = 4 };
        // in v1, comparable to Serializer.NonGeneric.SerializeWithLengthPrefix(ms, b, PrefixStyle.Base128, typeToTag[b.GetType()]);
        model.SerializeWithLengthPrefix(ms, b, null, PrefixStyle.Base128, typeToTag[b.GetType()]);
        model.SerializeWithLengthPrefix(ms, c, null, PrefixStyle.Base128, typeToTag[c.GetType()]);
        ms.Position = 0;
    
        // in v1, comparable to Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms, PrefixStyle.Base128, key => tagToType[key], out b2);
        object b2 = model.DeserializeWithLengthPrefix(ms, null, null, PrefixStyle.Base128, 0, key => tagToType[key]);
        object c2 = model.DeserializeWithLengthPrefix(ms, null, null, PrefixStyle.Base128, 0, key => tagToType[key]);
    
        Assert.AreEqual(((B)b).Y, ((B)b2).Y);
        Assert.AreEqual(((C)c).Y, ((C)c2).Y);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this happen? Please observe the following code: static class StringExtension { public
Observe the following .NET type: public class X { public DateTime Timestamp {get;set;} public
I wrote the following code snippet: void foo() { struct _bar_ { int a;
With a third party API I observed the following. Instead of using, public static
I have the following prototype JavaScript code Event.observe( window, 'load', function() { Event.observe( 'agreement',
Observe the following simple source code: using System; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit;
Let's observe the following segment of code in Java that uses System.currentTimeMillis() in a
Consider the following code : void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber) { boost::unique_lock<boost::mutex>(mtx); subscribers.push_back(subscriber); } void ListenerImpl::notify(MsgPtr
The following code is a test program wriiten to understand the behaviour of select()
Using static class members in a class is a common practice. consider the following

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.