Condividi tramite


Procedura: creare un'unione C/C++ tramite attributi (C# e Visual Basic)

Gli attributi consentono di personalizzare la modalità di disposizione delle struct nella memoria. È ad esempio possibile creare un'unione in C/C++ mediante gli attributi StructLayout(LayoutKind.Explicit) e FieldOffset.

Esempio

In questo segmento di codice tutti i campi di TestUnion iniziano nella stessa posizione di 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
    // 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;
        }

Nell'esempio riportato di seguito i campi iniziano in altre posizioni impostate in modo esplicito.

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

I due campi di tipo Integer i1 e i2 condividono le stesse posizioni di memoria di lg. Questo tipo di controllo sul layout delle struct è utile quando ci si avvale delle chiamate a basso livello.

Vedere anche

Riferimenti

Reflection (C# e Visual Basic)

Attributi (C# e Visual Basic)

Creazione di attributi personalizzati (C# e Visual Basic)

Accesso agli attributi tramite reflection (C# e Visual Basic)

System.Reflection

Attribute

Concetti

Guida per programmatori C#

Altre risorse

Guida per programmatori Visual Basic

Estensione di metadati mediante attributi