下面是一个样本:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Explicit)>
Public Structure TimeStamp32
<FieldOffset(0)>
Public I32 As Int32
<FieldOffset(0)>
<VBFixedArray(3)> Public Bytes() As Byte
End Structure
它给出了一个错误说&#34;无法加载类型TimeStamp32 ..blah blah ..因为它包含偏移0处的对象字段,该字段被非对象字段错误地对齐或重叠。&#34;虽然我可以将int与单个或任何其他4字节变量重叠,但没问题。
我实际上并不需要这个结构,但它可以帮助我管理我的128位ID结构,如果有一种方法可以帮助我管理它。
答案 0 :(得分:0)
我已将此标记为重复,但我会分享&#34;解决方法&#34;。
<StructLayout(LayoutKind.Explicit)>
Public Structure TimeStamp32
<FieldOffset(0)> Public Value As Integer
<FieldOffset(0)> Public Byte1 As Byte
<FieldOffset(1)> Public Byte2 As Byte
<FieldOffset(2)> Public Byte3 As Byte
<FieldOffset(3)> Public Byte4 As Byte
Public Sub New(value As Integer)
Me.Value = value
End Sub
Public Sub New(byte1 As Byte, byte2 As Byte, byte3 As Byte, byte4 As Byte)
Me.Byte1 = byte1
Me.Byte2 = byte2
Me.Byte3 = byte3
Me.Byte4 = byte4
End Sub
Public ReadOnly Property Bytes() As Byte()
Get
Return New Byte() {Me.Byte1, Me.Byte2, Me.Byte3, Me.Byte4}
End Get
End Property
End Structure