public static object ConvertToOneDimension <T>(byte[] bytes) where T : struct { var result = new T[bytes.Length / PrimitiveSizeHelper.SizeOf(typeof(T))]; Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); return(result); }
public static byte[] ToBytesArray(this Array array, out int[][] structure) { Type baseType = GetArrayBesicType(array); if (!baseType.IsPrimitive && baseType != typeof(decimal)) { throw new ArgumentException($"This function supports primitive and decimal types only. (Provided base type was '{baseType}')"); } if (baseType == typeof(decimal)) { throw new NotImplementedException("oh decimals..."); } int sizeOfBaseType = PrimitiveSizeHelper.SizeOf(baseType); int multiplyFactor = sizeOfBaseType / sizeof(byte); structure = array.GetArrayStructure(out _); byte[] bytesResult = new byte[GetNumberOfArrayElements(structure) * multiplyFactor]; int bytesOffset = 0; ToBytesArray(array, multiplyFactor, structure, 0, ref bytesResult, ref bytesOffset); return(bytesResult); }
public static object ConvertToJaggedArray(byte[] bytes, Type type, int firstDimensionSize) { Array result = InitializeJaggedArray(typeof(double[]), 0, new[] { firstDimensionSize, firstDimensionSize == 0 ? 0 : bytes.Length / PrimitiveSizeHelper.SizeOf(type) / firstDimensionSize }); // Array result = Array.CreateInstance(type, firstDimensionSize, firstDimensionSize == 0 ? 0 : bytes.Length / SizeHelper.SizeOf(type) / firstDimensionSize); var sourceOffSet = 0; for (var index = 0; index < result.Length; index++) { // result.SetValue(Array.CreateInstance(type, new[] { bytes.Length / SizeHelper.SizeOf(type) / firstDimensionSize }), new[] { index }); Buffer.BlockCopy(bytes, sourceOffSet, (Array)result.GetValue(new[] { index }), 0, bytes.Length / firstDimensionSize); sourceOffSet += bytes.Length / firstDimensionSize; } return(result); }