Cómo: Crear una unión en C/C++ mediante atributos (C# y Visual Basic)

Mediante los atributos, es posible personalizar cómo se organizan los structs en la memoria. Por ejemplo, se puede crear lo que se conoce en C/C++ como unión mediante los atributos StructLayout(LayoutKind.Explicit) y FieldOffset.

Ejemplo

En este segmento de código, todos los campos de TestUnion comienzan en la misma ubicación de la memoria.

<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
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct TestUnion
{
    [System.Runtime.InteropServices.FieldOffset(0)]
    public int i;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public double d;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public char c;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte b;
}

A continuación se da otro ejemplo donde los campos comienzan en diferentes ubicaciones establecidas de manera explícita.

 <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       
[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct TestExplicit
{
    [System.Runtime.InteropServices.FieldOffset(0)]
    public long lg;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public int i1;

    [System.Runtime.InteropServices.FieldOffset(4)]
    public int i2;

    [System.Runtime.InteropServices.FieldOffset(8)]
    public double d;

    [System.Runtime.InteropServices.FieldOffset(12)]
    public char c;

    [System.Runtime.InteropServices.FieldOffset(14)]
    public byte b;
}

Los dos campos de enteros, i1 e i2, comparten las mismas posiciones de memoria que lg. Esta clase de control sobre la organización de un struct es útil cuando se utilizan llamadas de la plataforma.

Vea también

Referencia

Reflexión (C# y Visual Basic)

Atributos (C# y Visual Basic)

Crear atributos personalizados (C# y Visual Basic)

Obtener acceso a los atributos mediante la reflexión (C# y Visual Basic)

System.Reflection

Attribute

Conceptos

Guía de programación de C#

Extender metadatos mediante atributos

Otros recursos

Guía de programación en Visual Basic