【问题标题】:How to use System.IO.BinaryWriter in C# with array of template/generic type?如何在 C# 中使用带有模板/泛型类型数组的 System.IO.BinaryWriter?
【发布时间】:2016-10-23 15:44:23
【问题描述】:

我想知道是否有任何方法可以在 C# 中使用 System.IO.BinaryWriter 来编写模板/泛型类型的数组?

例如,我在模板结构中有一个缓冲区:

T[] buffer

T 通常是 boolbyte。这个想法是有编写这些类型的方法,例如:

public void WriteByte(System.IO.BinaryWriter writer, int sizeToWrite)
{
  if (typeof(T) != typeof(byte)) Error.Happened("Struct is not of type byte.");

  // Direct use does not work even when T is 'byte'
  writer.Write(buffer[i], 0, sizeToWrite);

  // Casting does not work
  writer.Write((byte[])buffer[i], 0, sizeToWrite);
}

但是,似乎没有办法使用模板化数组进行写入。

非常欢迎任何建议!

【问题讨论】:

    标签: c# templates generics io binarywriter


    【解决方案1】:

    我不太确定目的是什么,但可以这样:

    public static void WriteByte<T>(T[] data, Converter<T, byte[]> converter, string path)
    {
      using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
      {
        using (BinaryWriter writer = new BinaryWriter(stream))
        {
          foreach (var x in data)
          {
            var bytes = converter(x);
            foreach (var b in bytes)
            {
              writer.Write(b);
            }
          }
        }
      }
    }
    

    或者更简单

    public static void WriteBytes<T>(T[] data, Converter<T, byte[]> converter, string path)
    {
      using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
      {
        foreach (T x in data)
        {
          var buffer = converter(x);
          stream.Write(buffer, 0, buffer.Length);
        }
      }
    }
    

    测试用例:

    static void Main(string[] args)
    {
      byte[] data = { 1, 2, 3, 4, 5, 6, 7 };
      WriteByte(data, (b) => new byte[] { b }, @"C:\Temp\MyBinary1.myb");
    
      int[] intData = { 1, 2, 3, 4, 5, 6, 7 };
      WriteByte(intData, BitConverter.GetBytes, @"C:\Temp\MyBinary2.myb");
    
      long[] longData = { 1, 2, 3, 4, 5, 6, 7 };
      WriteByte(longData, BitConverter.GetBytes, @"C:\Temp\MyBinary3.myb");
    
      char[] charData = { '1', '2', '3', '4', '5', '6', '7' };
      WriteByte(charData, BitConverter.GetBytes, @"C:\Temp\MyBinary4.myb");
    
      string[] stringData = { "1", "2", "3", "4", "5", "6", "7" }; 
      WriteByte(stringData, Encoding.Unicode.GetBytes, "C:\Temp\MyBinary5.myb");
    
    
    }
    

    编辑:

    另一种方法可能是这样的:

        public static void WriteBytes3<T>(T[] data, Action<T> writer)
        {
          foreach (T x in data)
          {
            writer(x);
          }
        }
    
        static void Main(string[] args)
        {
    
          using (FileStream stream = new FileStream(@"C:\Temp\MyBinary6.myb", FileMode.Create, FileAccess.Write, FileShare.None))
          using (BinaryWriter writer = new BinaryWriter(stream))
          {
            WriteBytes3(intData, writer.Write);
          }
        }
    

    【讨论】:

      【解决方案2】:
      for(int i = 0; i < sizeToWrite; ++i)
      {
          writer.Write((byte)buffer[i]);
      }
      

      或者更高效

      writer.Write(Array.ConvertAll(buffer, b => (byte)b), 0, sizeToWrite);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多