Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic BinaryReader

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.

like image 537
Andrew Cooper Avatar asked Mar 27 '12 03:03

Andrew Cooper


1 Answers

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.)

like image 68
porges Avatar answered Oct 03 '22 15:10

porges