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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:01:50+00:00 2026-05-23T12:01:50+00:00

Let’s say I have a Person class. It has an editable Notes property. I

  • 0

Let’s say I have a Person class. It has an editable Notes property.
I want to serialize the Person instance to a fixed size buffer, thus the Notes cannot be infinite long.
In the UI, I use a TextBox to let user edit the notes. I want to dynamically update a label saying how many more characters you can write.

This is my current implementation, is there any faster method? (I’m using rs282)

    public Int32 GetSerializedLength()
    {
        Byte[] data;
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.SerializeWithLengthPrefix<Person>(ms, this, PrefixStyle.Base128);
            data = ms.ToArray();
        }
        using (MemoryStream ms = new MemoryStream(data))
        { 
            Int32 length = 0;
            if (Serializer.TryReadLengthPrefix(ms, PrefixStyle.Base128, out length))
                return length;
            else
                return -1;
        }
    }

EDIT: I’m confused about the internal lengh of the serialized data and the total length of the serialized data.
Here is my final version:

    private static MemoryStream _ms = new MemoryStream();
    public static Int64 GetSerializedLength(Person person)
    {
        if(null == person) return 0;
        _ms.SetLength(0);
        Serializer.Serialize<Person>(_ms, person);
        return _ms.Length;
    }
  • 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-23T12:01:50+00:00Added an answer on May 23, 2026 at 12:01 pm

    With the edit, it sounds like you want the length without serializing it (since if you want the length with serializing it, you’d just serialize it and check the .Length).

    Basically, no – this isn’t available. I know that some of the other implementations build this data eagerly, that is in part because they are constructing the buffered data all the time, where-as protobuf-net works from an object graph.

    protobuf-net does not do this – it builds the data by discovery during a single pass over the object graph. Is there a specific purpose you have in mind? Things can always be added (with effort, though).


    Re the issue of a notes (string) field that you don’t want to be over-sized; as a sanity check, note that protubuf uses UTF8 or string data, so personally I would just do:

    if(theText.Length > MAX || Encoding.UTF8.GetByteCount(theText) > MAX
           || GetSerializedLength(obj) > MAX)
    {
            //
    }
    

    note we’ve checked this a bit more cheaply in the obvious cases

    • 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 class like this: public class Person { private String
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a drive such as C:\ , and I want to
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have a property foo defined in my parent POM. Is it
Let's say i have an android device that has some extra buttons on it,
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.