@@ -13,8 +13,7 @@ public static T ReadStruct<T>(BinaryReader reader)
1313 {
1414 T outStruct ;
1515 int count = Marshal . SizeOf ( typeof ( T ) ) ;
16- byte [ ] readBuffer = new byte [ count ] ;
17- readBuffer = reader . ReadBytes ( count ) ;
16+ byte [ ] readBuffer = reader . ReadBytes ( count ) ;
1817 GCHandle handle = GCHandle . Alloc ( readBuffer , GCHandleType . Pinned ) ;
1918 outStruct = ( T ) Marshal . PtrToStructure ( handle . AddrOfPinnedObject ( ) , typeof ( T ) ) ;
2019 handle . Free ( ) ;
@@ -25,8 +24,7 @@ public static void ReadStructs<T>(BinaryReader reader, T[] elements)
2524 {
2625 int elementSize = Marshal . SizeOf ( typeof ( T ) ) ;
2726 int bytes = elementSize * elements . Length ;
28- byte [ ] readBuffer = new byte [ bytes ] ;
29- readBuffer = reader . ReadBytes ( bytes ) ;
27+ byte [ ] readBuffer = reader . ReadBytes ( bytes ) ;
3028 GCHandle handle = GCHandle . Alloc ( readBuffer , GCHandleType . Pinned ) ;
3129 var addr = handle . AddrOfPinnedObject ( ) ;
3230 for ( var i = 0 ; i < elements . Length ; i ++ )
@@ -47,6 +45,22 @@ public static void WriteStruct<T>(BinaryWriter writer, ref T inStruct)
4745 writer . Write ( writeBuffer ) ;
4846 }
4947
48+ public static void WriteStructs < T > ( BinaryWriter writer , T [ ] elements )
49+ {
50+ int elementSize = Marshal . SizeOf ( typeof ( T ) ) ;
51+ int bytes = elementSize * elements . Length ;
52+ byte [ ] writeBuffer = new byte [ bytes ] ;
53+ GCHandle handle = GCHandle . Alloc ( writeBuffer , GCHandleType . Pinned ) ;
54+ var addr = handle . AddrOfPinnedObject ( ) ;
55+ for ( var i = 0 ; i < elements . Length ; i ++ )
56+ {
57+ var elementAddr = new IntPtr ( addr . ToInt64 ( ) + elementSize * i ) ;
58+ Marshal . StructureToPtr ( elements [ i ] , elementAddr , true ) ;
59+ }
60+ handle . Free ( ) ;
61+ writer . Write ( writeBuffer ) ;
62+ }
63+
5064 public static NodeAttribute ReadAttribute ( NodeAttribute . DataType type , BinaryReader reader )
5165 {
5266 var attr = new NodeAttribute ( type ) ;
0 commit comments