Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Mediante el uso de atributos, puede personalizar cómo se diseñan los structs en la memoria. Por ejemplo, puede crear lo que se conoce como una unión en C/C++ utilizando los elementos StructLayout(LayoutKind.Explicit)
y FieldOffset
.
Ejemplo 1
En este segmento de código, todos los campos de TestUnion
empiezan en la misma ubicación de memoria.
' Add an Imports statement for System.Runtime.InteropServices.
<System.Runtime.InteropServices.StructLayout(
System.Runtime.InteropServices.LayoutKind.Explicit)>
Structure TestUnion
<System.Runtime.InteropServices.FieldOffset(0)>
Public i As Integer
<System.Runtime.InteropServices.FieldOffset(0)>
Public d As Double
<System.Runtime.InteropServices.FieldOffset(0)>
Public c As Char
<System.Runtime.InteropServices.FieldOffset(0)>
Public b As Byte
End Structure
Ejemplo 2
A continuación se muestra otro ejemplo en el que los campos comienzan en diferentes ubicaciones establecidas explícitamente.
' Add an Imports statement for System.Runtime.InteropServices.
<System.Runtime.InteropServices.StructLayout(
System.Runtime.InteropServices.LayoutKind.Explicit)>
Structure TestExplicit
<System.Runtime.InteropServices.FieldOffset(0)>
Public lg As Long
<System.Runtime.InteropServices.FieldOffset(0)>
Public i1 As Integer
<System.Runtime.InteropServices.FieldOffset(4)>
Public i2 As Integer
<System.Runtime.InteropServices.FieldOffset(8)>
Public d As Double
<System.Runtime.InteropServices.FieldOffset(12)>
Public c As Char
<System.Runtime.InteropServices.FieldOffset(14)>
Public b As Byte
End Structure
Los dos campos enteros y i1
i2
, comparten las mismas ubicaciones de memoria que lg
. Este tipo de control sobre el diseño de estructura es útil al usar la invocación de plataforma.