Je sais pas, ça pourrai être utile.
Avec cette classe on peut :
- récupérer un byte[] à partir d'un struct
- récupérer un struct à partir d'un byte[]
- idem en remplaçant byte[] par un stream
* A priori les struct doivent avoir l'attribut StructLayout(LayoutKind.Sequential)

using :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;

code :
Code c# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
public static class StructGetter
    {
        public static void WriteBlock(Stream Output, object structure)
        {
            byte[] buf = new byte[SizeOf(structure)];
            WriteBlock(Output, structure, buf);
        }
 
        public static void WriteBlock(Stream Output, object structure, byte[] buf)
        {
            GetBytes(structure, buf, 0, buf.Length);
            Output.Write(buf, 0, SizeOf(structure));
        }
 
        public static object ReadBlock(Stream Input, Type structure_type)
        {
            byte[] buf = new byte[Marshal.SizeOf(structure_type)];
            return ReadBlock(Input, structure_type, buf);
        }
 
        public static object ReadBlock(Stream Input, Type structure_type, byte[] buf)
        {
            if (Input.Read(buf, 0, Marshal.SizeOf(structure_type)) < Marshal.SizeOf(structure_type))
                throw new DreamShield.Data.InvalidDataException("Stream too short");
 
            return GetStruct(buf, 0, Marshal.SizeOf(structure_type), structure_type);
        }
 
        public static int SizeOf(object structure)
        {
            return Marshal.SizeOf(structure);
        }
 
        public static void GetBytes(object val,
                                      [In, Out] byte[] buffer,
                                     int offset,
                                    int bufsize)
        {
            if (val == null)
                throw new ArgumentNullException("val");
 
            int size = SizeOf(val);
            IntPtr p = Marshal.AllocHGlobal(size);
 
            try
            {
                Marshal.StructureToPtr(val, p, true);
                Marshal.Copy(p, buffer, offset, bufsize);
            }
            finally
            {
                Marshal.FreeHGlobal(p);
            }
        }
 
        public static object GetStruct(byte[] buffer,
                                       int offset,
                                       int count,
                                       Type struct_type)
        {
            IntPtr p = Marshal.AllocHGlobal(count);
            object result = null;
 
            try
            {
                Marshal.Copy(buffer, offset, p, count);
                result = Marshal.PtrToStructure(p, struct_type);
            }
            finally
            {
                Marshal.FreeHGlobal(p);
            }
 
            return result;
        }
    }