Comment : créer une union C/C++ à l'aide d'attributs (C# et Visual Basic)

L'utilisation d'attributs vous permet de personnaliser la présentation des structures dans la mémoire. Par exemple, vous pouvez créer ce qu'on appelle une union en C/C++ avec les attributs StructLayout(LayoutKind.Explicit) et FieldOffset.

Exemple

Dans ce segment de code, tous les champs de TestUnion commencent au même emplacement dans la mémoire.

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

Le code suivant est un autre exemple dans lequel les champs commencent à des emplacements différents définis de manière explicite.

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

Les deux champs de nombres entiers, i1 et i2, partagent les mêmes emplacements de mémoire que lg. Ce type de contrôle sur la disposition d'une structure est utile lorsque vous utilisez l'appel de code non managé.

Voir aussi

Référence

Réflexion (C# et Visual Basic)

Attributs (C# et Visual Basic)

Création d'attributs personnalisés (C# et Visual Basic)

Accès à des attributs à l'aide de la réflexion (C# et Visual Basic)

System.Reflection

Attribute

Concepts

Guide de programmation C#

Autres ressources

Guide de programmation Visual Basic

Extension des métadonnées à l'aide des attributs