Generic BinaryReader/Writer Read/Write methods I have an extension that implements all the BinaryReader/BinaryWriter in the Stream class. I want to create generic methods for them. Is there a nicer way of doing this?

public static void Write&lt;T&gt;(this Stream stream, T num)
{
    if (num is byte)
    {
        stream.Write((byte)(object)num);
    }
    else if (num is Int16)
    {
        stream.Write((Int16)(object)num);
    }
    else if (num is Int32)
    {
        stream.Write((Int32)(object)num);
    }
}

public static T Read&lt;T&gt;(this Stream stream)
{
    object ret = null;

    if (typeof(T) == typeof(byte))
        ret = stream.ReadInt8();
    else if (typeof(T) == typeof(Int16))
        ret = stream.ReadInt16();
    else if (typeof(T) == typeof(Int32))
        ret = stream.ReadInt32();

    if (ret == null)
        throw new ArgumentException("Unable to read type - " + typeof(T));
    return (T)ret;
}


Now using this for the Read method.

static Dictionary&lt;Type, Func&lt;Stream, object&gt;&gt; ReadFuncs = new Dictionary&lt;Type, Func&lt;Stream, object&gt;&gt;()
{
    {typeof(bool), s =&gt; s.ReadBoolean()},
    {typeof(byte), s =&gt; s.ReadInt8()},
    {typeof(Int16), s =&gt; s.ReadInt16()},
    {typeof(Int32), s =&gt; s.ReadInt32()},
    {typeof(Int64), s =&gt; s.ReadInt64()},
    {typeof(string), s =&gt; s.ReadString()},
};
public static T Read&lt;T&gt;(this Stream stream)
{
    if (ReadFuncs.ContainsKey(typeof(T)))
        return (T)ReadFuncs[typeof(T)](stream);

    throw new NotImplementedException();
}
I'm guessing you defined more write methods than this? Otherwise the Write() would call itself recursively infinitely. You probably have overloads for every supported type, making the generic write method superfluous. This would also explain the weird casting you are doing. I'm guessing an overloaded function with a specific type has precedence over a generic method, which is why it could work. This however, doesn't make it a clean solution.

Your Read() method seems to be more useful. Instead of throwing a ArgumentException I would prefer throwing a NotSupportedException.

As a sidenote, are you sure you want to make these extension methods of Stream and not some more specific stream? Stream can be any type of stream.



In what way is this an improvement over using BinaryWriter/BinaryReader? For the purposes I gather from your code I would only create a Read&lt;T&gt; extension method for BinaryReader.

If you feel like experimenting a bit, there might be a more performant/cleaner solution, although you'll have to benchmark to be sure. You could create a Dictionary&lt;Type, Func&lt;object&gt;&gt; to hold a matching 'reader' for every type. Initializing the delegates to store in this dictionary is the tough part, on which information can be found in this article. Most likely this is not worth the effort, although it still is a very interesting article. :) Be sure to also check out the same solution implemented by using Expression Trees in the comments of the article. For a similar implementation example I did, check out this question I posted on Code Review.