我有一個固定大小的結構,我試圖編組。此結構包含當前版本的結構的許多有用字段以及末尾未使用空間的指定數量,這些空間留作未來修改使用。
我應該如何設計這個結構,以便在修改結構時保留空間的大小會自動更新?
而下面將解決我的問題
'Variable size structure
<StructLayout(LayoutKind.Sequential, Pack:=1)>
Structure UsefulData
Dim foo As SByte
Dim bar As Integer
Dim foobar As Short
End Structure
Const MAX_SIZE As Integer = 20
'Fixed size structure
<StructLayout(LayoutKind.Sequential, Pack:=1, Size:=MAX_SIZE>
Structure Data
Dim current As UsefulData
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAX_SIZE-System.Runtime.InteropServices.Marshal.SizeOf(GetType(UsefulData)))>
Dim reserved As SByte()
End Structure
但不編譯爲System.Runtime.InteropServices.Marshal.SizeOf(GetType(UsefulData))
不是一個常量表達式。有任何想法嗎?