在泛型类中使用 BinaryWriter

[Serializable]

public class Vector3D<T> {

    public T x;

    public T y;

    public T z;

}

我有一个简单的 Vector 类,用于存储各种数值类型的坐标。


我需要从文件中读取它,然后再存储它。


它通常看起来像这样:


someCoord = new Vector3D<int> {

                x = reader.ReadInt32(),

                y = reader.ReadInt32(),

                z = reader.ReadInt32()

            };

writer.Write(someCoord.x);

writer.Write(someCoord.y);

writer.Write(someCoord.z);

我很想把这些线变成这样的东西:


someCoord = new Vector3D<int>(reader);

someCoord.Write(writer);

我会在 Vector3D 类中准备好 Read 和 Write 方法。虽然写作不是问题,因为我可以执行以下操作:


writer.Write((T)x)

尝试以相同的方式阅读成为一个问题,因为我必须调用具有不同名称的方法,具体取决于我想要阅读的类型。


说了这么多,是否有一些聪明的方法仍然可以实现这一目标?我正在考虑使用委托,但我无法弄清楚语法。


我有一本字典,我会在其中保留 Type-Function(delegate) 对,这样当我想阅读一些东西时,我会调用类似的东西:


T value = reader.dictionary[T]();

其中 reader 是 BinaryReader 的一个实例。如果 T 是 int,dictionary[T] 将替换为 readInt32,如果 T 是 float,dictionary[T] 将替换为 readSingle,等等。


知道如何实现这一目标吗?


慕村9548890
浏览 107回答 1
1回答

肥皂起泡泡

您可以创建一个依赖于 的泛型类型的泛型方法<T>,处理读取,将值装箱并最终返回它。例如:&nbsp; &nbsp; public static Object GetValue<T>(BinaryReader br) {&nbsp; &nbsp; &nbsp; &nbsp; switch (Type.GetTypeCode(typeof(T)))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Boolean: { return br.ReadBoolean(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Byte: { return br.ReadByte(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Char: { return br.ReadChar(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Decimal: { return br.ReadDecimal(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Double: { return br.ReadDouble(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int16: { return br.ReadInt16(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int32: { return br.ReadInt32(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int64: { return br.ReadUInt64(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Keep on going for other types&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: { return br.Read(); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这是一个完整的例子public class Vector3D<T>{&nbsp; &nbsp; public T X { get; set; }&nbsp; &nbsp; public T Y { get; set; }&nbsp; &nbsp; public T Z { get; set; }&nbsp; &nbsp; private readonly String filePath;&nbsp; &nbsp; public Vector3D(String filePath) {&nbsp; &nbsp; &nbsp; &nbsp; this.filePath = filePath;&nbsp; &nbsp; &nbsp; &nbsp; if (File.Exists(filePath))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using(BinaryReader br = new BinaryReader(fs))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X = (T) GetValue<T>(br);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Y = (T) GetValue<T>(br);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Z = (T) GetValue<T>(br);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static Object GetValue<T>(BinaryReader br) {&nbsp; &nbsp; &nbsp; &nbsp; switch (Type.GetTypeCode(typeof(T)))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Boolean: { return br.ReadBoolean(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Byte: { return br.ReadByte(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Char: { return br.ReadChar(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Decimal: { return br.ReadDecimal(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Double: { return br.ReadDouble(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int16: { return br.ReadInt16(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int32: { return br.ReadInt32(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int64: { return br.ReadUInt64(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: { return br.Read(); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您还可以为此创建扩展方法:public static class BinaryReaderExtensions{&nbsp; &nbsp; public static Object GetValue(this BinaryReader br, Type type)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch (Type.GetTypeCode(type))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Boolean: { return br.ReadBoolean(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Byte: { return br.ReadByte(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Char: { return br.ReadChar(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Decimal: { return br.ReadDecimal(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Double: { return br.ReadDouble(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int16: { return br.ReadInt16(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int32: { return br.ReadInt32(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TypeCode.Int64: { return br.ReadUInt64(); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: { return br.Read(); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP