Прочитать бинарный файл в структуру

Вот что я использую.
Это .cs-file сработало для меня при чтении struct Portable Executable Format.
Это io общая функция, поэтому T - это c#.net ваш тип struct.

public static T ByteToType(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;
}

c#

struct

io

binaryfiles

2022-11-13T04:15:50+00:00
Вопросы с похожей тематикой, как у вопроса:

Прочитать бинарный файл в структуру