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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:52:48+00:00 2026-05-31T22:52:48+00:00

I’m writing binary serialiser/deserialser to convert a number of object types to/from a byte

  • 0

I’m writing binary serialiser/deserialser to convert a number of object types to/from a byte stream. The objects represent API commands and their associated responses for a device connected by Bluetooth or USB. I’m using the BinaryWriter & BinaryReader to write/read to/from the stream.

The serialiser is easy. The properites to be serialised are tagged with an attribute that specifies the order in which they are to be written to the byte stream. I iterate through the properties using reflection and overload resolution handles picking the correct Write(...) method of the BinaryWriter.

The deserialiser is not quite so simple. Again I can iterate through the properites in the particular response class that I’m expecting to determine the types that need to be read from the stream. The tricky bit is picking the correct method to call on the BinaryReader to read the value I need. I’ve thought of two approaches.

  1. A big switch statement that calls the correct ReadXXXX() method based on the type to be read.
  2. Use the name of the type I need to build the name of the method I need in a string, and then invoke the method using relection.

Is there a simpler way I’m not thinking of? It’s just a shame you can’t do overload resolution based on the return type you want.

  • 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-31T22:52:50+00:00Added an answer on May 31, 2026 at 10:52 pm

    I’ve used option 1 (big switch statement) in a binary deserializer. A cleaner method could be something like:

    {
        object result;
    
        BinaryReader ...;
        foreach (var propertyInfo in ...)
        {
            Func<BinaryReader, object> deserializer;
            if (!supportedTypes.TryGetValue(propertyInfo.PropertyType, out deserializer))
            {
                throw new NotSupportedException(string.Format(
                    "Type of property '{0}' isn't supported ({1}).", propertyInfo.Name, propertyInfo.PropertyType));
            }
    
            var deserialized = deserializer(reader);
            propertyInfo.SetValue(result, deserialized, null);
        }
    }
    
    private static Dictionary<Type, Func<BinaryReader, object>> supportedTypes = new Dictionary<Type, Func<BinaryReader, object>>
    {
        { typeof(int), br => br.ReadInt32() },
        // etc
    };
    

    Another option is to let the command classes themselves do the serialization:

    interface IBinarySerializable
    {
        void Serialize(BinaryWriter toStream);
        void Deserialize(BinaryReader fromStream);
    }
    

    Then in your commands:

    abstract class Command : IBinarySerializable
    {
    
    }
    
    class SomeCommand : Command
    {
        public int Arg1 { get; set; }
    
        public void Serialize(BinaryWriter toStream)
        {
            toStream.Write(Arg1);
        }
    
        public void Deserialize(BinaryReader fromStream)
        {
            Arg1 = fromStream.ReadInt32();
        }
    }
    

    And generic serialization methods:

    void Serialize<T>(T obj) where T : IBinarySerializable
    {
        obj.Serialize(_stream);
    }
    
    T Deserialize<T>() where T : new(), IBinarySerializable
    {
        var result = new T();
        result.Deserialize(_stream);
        return result;
    }
    

    But this way you might end up duplicating some code. (On the other hand, derived classes can call their parent class versions of Serialize/Deserialize if that makes sense in your scenario, and that works nicely.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
I am writing an app with both english and french support. The app requests

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.