Compartilhar via


Como criar uma união do C/C++ usando atributos (C# e Visual Basic)

Usando atributos, você pode personalizar como estruturas são dispostas na memória. Por exemplo, você pode criar o que é conhecido como uma união em C/C++ usando o StructLayout(LayoutKind.Explicit) e FieldOffset atributos.

Exemplo

No segmento de código, todos os campos de TestUnion iniciar no mesmo local na memória.

' 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
    // Add a using directive for System.Runtime.InteropServices.

        [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;
        }

Este é outro exemplo onde iniciar de campos em diferentes explicitamente definir locais.

 ' 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       
    // Add a using directive for System.Runtime.InteropServices.

        [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;
        }

Os campos de dois inteiros, i1 e i2, compartilhar os mesmos locais de memória como lg. Esse tipo de controle sobre o layout de struct é útil ao usar invocação de plataforma.

Consulte também

Referência

Reflexão (C# e Visual Basic)

Atributos (C# e Visual Basic)

Criando atributos personalizados (C# e Visual Basic)

Acessando atributos usando reflexão (C# e Visual Basic)

System.Reflection

Attribute

Conceitos

Guia de Programação em C#

Outros recursos

Guia de programação do Visual Basic

Estendendo metadados por meio de atributos