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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:55:52+00:00 2026-06-02T03:55:52+00:00

I am using the following class to serialize individual objects (T) with a prefix,

  • 0

I am using the following class to serialize individual objects (T) with a prefix, and then attempting to deserialize back to a list w/ the DeserializeItems() method:

public class ProtobufSerializationProvider<T> : ISerializationProvider<T>
{
    readonly Type type; 
    readonly TypeModel model;

    public ProtobufSerializationProvider()
    {
        this.type = typeof(T); 
        this.model = createModel(this.type);
    }

    public byte[] Serialize(T instance)
    {
        byte[] buffer; 
        using (MemoryStream strm = new MemoryStream())
        {
            model.SerializeWithLengthPrefix
              (strm, instance, type, PrefixStyle.Base128, 1);

            buffer = strm.GetBuffer();              
        }
        return buffer; 
    }

    // here is the problem method
    public IEnumerable<T> DeserializeAll(MemoryStream stream)
    {
        return model.DeserializeItems<T>(stream, PrefixStyle.Base128, 1); 
    }

    TypeModel createModel(Type type)
    {
        try
        {
            RuntimeTypeModel runtimeModel = TypeModel.Create();
            this.addTypeToModel(runtimeModel, type);
            this.addTypePropertiesToModel(runtimeModel, type);
            return runtimeModel.Compile();
        }
        catch (Exception e)
        {
            throw e.InnerException;
        }
    }

    void addTypePropertiesToModel(RuntimeTypeModel typeModel, Type type)
    {
        PropertyInfo[] properties = type.GetProperties();
        for (int i = 0; i < properties.Length; i++)
        {
            Type innerType = properties[i].PropertyType;
            if (!innerType.IsPrimitive && !(innerType == typeof(string)))
            {
                addTypeToModel(typeModel, properties[i].PropertyType);
            }
        }
    }

    MetaType addTypeToModel(RuntimeTypeModel typeModel, Type t)
    {                
        var properties = t.GetProperties()
            .Select(p => p.Name)
            .OrderBy(name => name);

        return typeModel
            .Add(t, true)
            .Add(properties.ToArray());            
    }
}

I am getting a default InvalidOperationException “Operation is not valid due to the current state of the object” when I try to enumerate the IEnumerable, whether by casting ToList(), or counting it Count(), etc. Specifically the MoveNext() method will throw the error:

enumerator.MoveNext()

Also the stream has to be open until DeserializeItems(stream) returns, and I ensured that was the case. But once the IEnumerable successfully returns I cannot use it.

Not sure if there is a problem with the serialized items or not. Also I noticed that each of my items 256 bytes, although a large portion of those bytes are just trailing nulls.

This is the class I am serializing as a test. Note that I am not using attributes since I create the model manually:

public class NestedFoo
{
    public string NestedFooStr { get; set; } 
}

public class Foo
{
    public string Foo1 { get; set; }
    public string Foo2 { get; set; }
    public string Foo3 { get; set; }
    public string Foo4 { get; set; }

    public NestedFoo NestedFoo { get; set; }
}

Thanks.

UPDATE

After switching from MemoryStream.GetBuffer() to MemoryStream.ToArray(), the message is shortened but trying to cast the IEnumerable .ToList() or any other operation produces the same error.

However, I have noticed that after switching to .ToArray() the ‘Current’ property of the IEnumerable after enumeration reaches the last element in the IEnumerable before throwing the error, whereas when using .GetBuffer() the ‘Current’ property on the first element when the error is thrown.

I suspect the problem could be that the IEnumerable<> doesn’t know when its out of elements because its reached the end of the stream? I append several items serialized with SerializeWithLengthPrefix() to a single byte[], and then there may be nulls left over trailing the array. If I read that entire array into the DeserializeItems method, does it need to know how to terminate or does it only continue if it detects a length prefix (as I would expect?).

Also one other question I had was to what extent using the SerializeWithLengthPrefix() method is equivalent to just serializing a List in one operation, using the DataFormat.Group enumeration, like:

[ProtoContract]
public class ProtoList<T>
{
    [ProtoMember(1, DataFormat = DataFormat.Group)]
    public List<T> List { get; set; }

    public ProtoList() { }

    public ProtoList(IEnumerable<T> items)
    {
        this.List = new List<T>(items); 
    }
}

Using a container such as this seems to perform very well, so I may just go with this route in cases where I want to serialize a list in one shot.

However for serializing items individually as they are added I am currently using my own prefixing function, so if I can get the Protobuf prefixing methods to work that would be good too.

  • 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-02T03:55:54+00:00Added an answer on June 2, 2026 at 3:55 am

    The 256/trailing nulls is because you are using MemoryStream.GetBuffer(). This method is fine as long as it is used in combination with the Length, as the buffer is oversized. If you want a right-sized array, use ToArray() instead. Alternatively use ArraySegment<byte> to avoid copying any data.

    It is possible this also fixes the exception (0 is not a valid value for a field-header). If not, leave a comment so I know to look in more detail. If it does fix it, please also let me know, and I’ll try to make the error message (for the 0 field-header case) more helpful.

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

Sidebar

Related Questions

I am using C# and XmlSerializer to serialize the following class: public class Title
I am trying to register the following class using the fluent interface: public class
I'm using the following mapping: public class LoadMap : IAutoMappingOverride<Load> { public void Override(AutoMapping<Load>
Using the following Webservice definition using aClientArgs as a complex type: [System.Web.Script.Services.ScriptService] public class
I'm using JSON.NET implementation to serialize/deserialize .NET objects to JS and vice versa, all
How would you serialize/deserialize this class using boost::serialization? #include <vector> struct Foo { struct
How can I XML-serialize the following property: public class SomeCollection { [Xml???] public SomeClass
I have the following method to serialize a list of classes to disk: private
I am using the following code to Serialize / Deserialize from object to XML
I am using following class to show a grid like appearance with pagination on

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.