I have an array of different type objects and I use a BinaryWriter to convert each item to its binary equivalent so I can send the structure over the network.
I curr
Here is a solution for BinaryWriter that uses reflection.
This basically scans BinaryWriter for methods named Write that takes exactly one parameter, then builds a dictionary of which method handles which type, then for each object to write, finds the right method and calls it on the writer.
Dirty, and you should probably look for better ways of doing the whole thing (not just the writing part), but it should work for your current needs:
using System.IO;
using System;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApplication14
{
public class Program
{
public static void Main()
{
Dictionary mapping = new Dictionary();
foreach (MethodInfo mi in typeof(BinaryWriter).GetMethods())
{
if (mi.Name == "Write")
{
ParameterInfo[] pi = mi.GetParameters();
if (pi.Length == 1)
mapping[pi[0].ParameterType] = mi;
}
}
List