public static Real[] GetArray(Array data) { Type arrayType = data.GetType().GetElementType(); Real[] resultData = new Real[data.Length]; //型の不一致をここで吸収 if (arrayType != typeof(RealType) && arrayType != typeof(Real)) { //一次元の長さの配列を用意 Array array = Array.CreateInstance(arrayType, data.Length); //一次元化して Buffer.BlockCopy(data, 0, array, 0, Marshal.SizeOf(arrayType) * resultData.Length); data = new RealType[array.Length]; //型変換しつつコピー Array.Copy(array, data, array.Length); } //データを叩き込む int size = sizeof(RealType) * data.Length; GCHandle gchObj = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle gchBytes = GCHandle.Alloc(resultData, GCHandleType.Pinned); RealTool.CopyMemory(gchBytes.AddrOfPinnedObject(), gchObj.AddrOfPinnedObject(), size); gchObj.Free(); gchBytes.Free(); return(resultData); }
public static Real[] GetArray(Array data) { Type arrayType = data.GetType().GetElementType(); Real[] resultData = new Real[data.Length]; //Absorption of type mismatch here if (arrayType != typeof(RealType) && arrayType != typeof(Real)) { //Prepare one-dimensional length array Array array = Array.CreateInstance(arrayType, data.Length); //Make it one-dimensional Buffer.BlockCopy(data, 0, array, 0, Marshal.SizeOf(arrayType) * resultData.Length); data = new RealType[array.Length]; //Copy while converting type Array.Copy(array, data, array.Length); } //Strike data int size = Marshal.SizeOf(typeof(RealType)) * data.Length; GCHandle gchObj = GCHandle.Alloc(data, GCHandleType.Pinned); GCHandle gchBytes = GCHandle.Alloc(resultData, GCHandleType.Pinned); RealTool.CopyMemory(gchBytes.AddrOfPinnedObject(), gchObj.AddrOfPinnedObject(), size); gchObj.Free(); gchBytes.Free(); return(resultData); }