I want to extend the BinaryWriter class to be able to write a list to a stream.
I want to do this with multiple types of lists. I set up this generic function as an extension
public static void Write<T>(this BinaryWriter source, IEnumerable<T>items)
{
foreach (T item in items)
source.Write(item)//This doesn't work
}
Is this at all possible? I know write can handle all the built in types. I know there is the ability to constrain T to certain types, but I couldn’t do it for int and double.
I only need it to work for ints, doubles, and bytes.
dasblinkenlight’s solution is probably the way to go, but here’s an alternative:
For more info on
dynamic, see the documentation.