Hallo, ich habe eine Extension-Method geschrieben um mehr Typen als Standard mit den BinaryReader/Writer zu nutzen.
Leider ist es zu viel Arbeit jeden Typ von Array abzudecken.
Gibt es eine Möglichkeit mithilfe von Generics jedes Array abzudecken?
Mein Code:
private static Dictionary<Type, Func<object>> InitReader(BinaryReader br)
{
var _Readers = new Dictionary<Type, Func<object>>
{
{typeof(Dictionary<string, byte[]>), () =>
{
var l = new Dictionary<string, byte[]>();
for (var i = 0; i < br.Read<int>(); i++)
{
l.Add(br.Read<string>(), br.Read<byte[]>());
}
return l;
}},
{typeof(int[]), () =>
{
var c = br.Read<int>();
var a = new int[c];
for (var i = 0; i < c; i++)
{
a[i] = br.Read<int>();
}
return a;
}},
{typeof(Enum), () =>
{
return br.Read<int>();
}},
{typeof (byte), () => br.ReadByte()},
{typeof (bool), () => br.ReadBoolean()},
{typeof (byte[]), br.Readbytes},
{typeof (char), () => br.ReadChar()},
{typeof (decimal), () => br.ReadDecimal()},
{typeof (double), () => br.ReadDouble()},
{typeof (Int16), () => br.ReadInt16()},
{typeof (int), () => br.ReadInt32()},
{typeof (Int64), () => br.ReadInt64()},
{typeof (sbyte), () => br.ReadSByte()},
{typeof (float), () => br.ReadSingle()},
{typeof (string), br.ReadString},
{typeof (UInt16), () => br.ReadUInt16()},
{typeof (uint), () => br.ReadUInt32()},
{typeof (UInt64), () => br.ReadUInt64()},
{typeof(Color), () => Color.FromArgb(br.ReadInt32(),br.ReadInt32(),br.ReadInt32(),br.ReadInt32())},
{typeof(Point), () => new Point(br.ReadInt32(),br.ReadInt32())},
{typeof(Size), () => new Size(br.ReadInt32(),br.ReadInt32())},
{typeof (DateTime), () => DateTime.Parse(br.ReadString())},
{typeof (Stream), () => new MemoryStream(br.Readbytes())}
};
return _Readers;
}
private static Dictionary<Type, Action> InitWriter(BinaryWriter br, object value)
{
var _Readers = new Dictionary<Type, Action>
{
{typeof(Dictionary<string, byte[]>), () =>
{
var val = (Dictionary<string, byte[]>)value;
br.Write<int>(val.Count);
foreach (var v in val)
{
br.Write<string>(v.Key);
br.Write<byte[]>(v.Value);
}
}},
{typeof (byte), () => br.Write((byte) value)},
{typeof(Enum), () =>
{
br.Write((int)value);
}},
{typeof (bool), () => br.Write((bool) value)},
{typeof (byte[]), () => br.Writebytes((byte[]) value)},
{typeof (char[]), () =>
{
var c = (char[]) value;
br.Write(c.Length);
foreach (var cc in c)
{
br.Write(cc);
}
}},
{typeof (int[]), () =>
{
var c = (int[]) value;
br.Write(c.Length);
foreach (var cc in c)
{
br.Write(cc);
}
}},
{typeof (string[]), () =>
{
var c = (string[]) value;
br.Write(c.Length);
foreach (var cc in c)
{
br.Write(cc);
}
}},
{typeof (char), () => br.Write((char) value)},
{typeof (decimal), () => br.Write((decimal) value)},
{typeof (double), () => br.Write((double) value)},
{typeof (Int16), () => br.Write((Int16) value)},
{typeof (int), () => br.Write((int) value)},
{typeof (Int64), () => br.Write((Int64) value)},
{typeof (sbyte), () => br.Write((sbyte) value)},
{typeof (float), () => br.Write((float) value)},
{typeof (string), () => br.Write(((string) value))},
{typeof (UInt16), () => br.Write((UInt16) value)},
{typeof (uint), () => br.Write((uint) value)},
{typeof (UInt64), () => br.Write((UInt64) value)},
{typeof(Color), () =>
{
var c = (Color)value;
br.Write(c.A);
br.Write(c.R);
br.Write(c.G);
br.Write(c.B);
}},
{typeof(Point), () =>
{
var c = (Point)value;
br.Write(c.X);
br.Write(c.Y);
}},
{typeof(Size), () =>
{
var c = (Size)value;
br.Write(c.Width);
br.Write(c.Height);
}},
{typeof (DateTime), () => br.Write(((DateTime)value).ToString())},
{typeof (Stream), () => br.Writebytes(((MemoryStream)value).ToArray())}
};
return _Readers;
}
[DebuggerStepThrough]
public static TT Read<TT>(this BinaryReader br, Type T)
{
var readers = InitReader(br);
if(!readers.ContainsKey(T))
throw new NotSupportedException(T.Name + " is not supported");
else
return (TT) readers[T]();
}
public static T Read<T>(this BinaryReader br)
{
return Read<T>(br, typeof (T));
}
Wäre für ne Möglichkeit zur Lösung dankbar
vielen Dank
C. Anders
Irgendwie im Prinzip doch die gleichen Antworten wie Du sie bereits in Extendet BinaryReader/Writer: Welche Klassen könnte man sonst noch binär speichern? erhalten hast: eine Allround-Variante für alles gibt es nicht.
Das sieht man doch schon allein an der nötigen Arbeit zwischen Structs und Color o.ä.
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code