short temp = *((short[])info.origDataBuff + k * 12 + l);
编译不过Error 3 无法将类型“byte[]”转换为“short []”
这里info.origDataBuffer是 byte[],而需要按照short[]来访问,在C里面这就是个普通的类型强制转换,C#初学,就不明白了。希望明白的人给说说。还有像OleDbDataReader reader,的reader.GetBytes要得是 byte[],但是我想给个short[],强制了也是不行。

解决方案 »

  1.   

    int[,] arr = new int[2, 2] { { 1, 2 }, { 3, 4 } };
    int[] arr1 = new int[2];
    IntPtr arrHandler = Marshal.UnsafeAddrOfPinnedArrayElement(arr, 2);
    Marshal.Copy(arrHandler, arr1, 0, arr1.Length);public static byte[] StructToBytes(object Obj)
      {   
      int size = Marshal.SizeOf(Obj);   
      byte[] buffer= new byte[size];   
      IntPtr intPtr = Marshal.AllocHGlobal(size);   
      Marshal.StructureToPtr(Obj, intPtr , false);   
      Marshal.Copy(intPtr , buffer, 0, size);   
      Marshal.FreeHGlobal(_StructIntPtr);   
      return buffer;
      }
      

  2.   

    我明白这个函数的意思了,不过我想直接强制转换受控的引用不行吧。例如 void test(byte[] data);short [] data2=new short[100];test((short[])data2);这种应用方式,是不是必须用个中间数组进行转储?
      

  3.   

    我想了下,打算用BitConverter来将byte[]逐个转换为short,不知道何时不合适。这样不用分配中间变量毕竟我这个数组大约800K左右。而且可能是多实例在跑。