Freigeben über


Gewusst wie: Erstellen einer Union in C/C++ mit Attributen (C# und Visual Basic)

Durch die Verwendung von Attributen können Sie das Layout von Strukturen im Speicher anpassen. Sie können beispielsweise ein in C/C++ als Union bezeichnetes Element erstellen, indem Sie das StructLayout(LayoutKind.Explicit)-Attribut und das FieldOffset-Attribut verwenden.

Beispiel

In diesem Codesegment beginnen alle Felder von TestUnion an derselben Speicherposition.

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

Im Folgenden sehen Sie ein Beispiel für Felder, die an anderen, explizit festgelegten Speicherpositionen starten.

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

Die beiden Ganzzahlfelder i1 und i2 besitzen dieselbe Speicherposition wie lg. Diese Kontrolle über das Strukturlayout ist besonders bei Verwendung von Plattformaufrufen hilfreich.

Siehe auch

Referenz

Reflektion (C# und Visual Basic)

Attribute (C# und Visual Basic)

Erstellen benutzerdefinierter Attribute (C# und Visual Basic)

Zugreifen auf Attribute mithilfe der Reflektion (C# und Visual Basic)

System.Reflection

Attribute

Konzepte

C#-Programmierhandbuch

Erweitern von Metadaten mithilfe von Attributen

Weitere Ressourcen

Visual Basic-Programmierhandbuch