using System.IO; using System.Runtime.InteropServices; namespace MindPowerSdk { public static class StructReader { public static T ReadStruct(BinaryReader reader) { byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T))); GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); handle.Free(); return theStructure; } public static T ReadStructFromArray(byte[] bytes) { GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); handle.Free(); return theStructure; } public static T[] ReadStructArray(BinaryReader reader, int size) { T[] theStructures = new T[size]; for (int i = 0; i < size; i++) { theStructures[i] = ReadStruct(reader); } return theStructures; } public static T[] ReadStructArray(BinaryReader reader, uint size) { T[] theStructures = new T[size]; for (uint i = 0; i < size; i++) { theStructures[i] = ReadStruct(reader); } return theStructures; } } }