LayoutKind Énumération

Définition

Contrôle la disposition d'un objet exporté vers du code non managé.

public enum class LayoutKind
public enum LayoutKind
[System.Serializable]
public enum LayoutKind
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum LayoutKind
type LayoutKind = 
[<System.Serializable>]
type LayoutKind = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type LayoutKind = 
Public Enum LayoutKind
Héritage
LayoutKind
Attributs

Champs

Auto 3

Le runtime sélectionne automatiquement une disposition appropriée pour les membres d'un objet en mémoire non managée. Les objets définis avec ce membre d'énumération ne peuvent être exposés en dehors du code managé. Une telle tentative génère une exception.

Explicit 2

La position précise de chaque membre d'un objet dans la mémoire non managée est explicitement contrôlée, en fonction du paramètre du champ Pack. Chaque membre doit utiliser FieldOffsetAttribute pour indiquer la position de ce champ dans le type.

Sequential 0

Les membres de l'objet sont disposés séquentiellement, en respectant l'ordre dans lequel ils apparaissent lors de l'exportation dans la mémoire non managée. Les membres sont disposés selon la compression spécifiée dans Pack et peuvent être non contigus.

Exemples

L’exemple suivant montre la déclaration managée de la PtInRect fonction, qui vérifie si un point se trouve dans un rectangle et définit une structure avec une Point disposition séquentielle et une Rect structure avec une disposition explicite.

enum class Bool
{
   False = 0,
   True
};


[StructLayout(LayoutKind::Sequential)]
value struct Point
{
public:
   int x;
   int y;
};


[StructLayout(LayoutKind::Explicit)]
value struct Rect
{
public:

   [FieldOffset(0)]
   int left;

   [FieldOffset(4)]
   int top;

   [FieldOffset(8)]
   int right;

   [FieldOffset(12)]
   int bottom;
};

ref class NativeMethods
{
public:

   [DllImport("user32.dll",CallingConvention=CallingConvention::StdCall)]
   static Bool PtInRect( Rect * r, Point p );
};

int main()
{
   try
   {
      Bool bPointInRect = (Bool)0;
      Rect myRect = Rect(  );
      myRect.left = 10;
      myRect.right = 100;
      myRect.top = 10;
      myRect.bottom = 100;
      Point myPoint = Point(  );
      myPoint.x = 50;
      myPoint.y = 50;
      bPointInRect = NativeMethods::PtInRect(  &myRect, myPoint );
      if ( bPointInRect == Bool::True )
            Console::WriteLine( "Point lies within the Rect" );
      else
            Console::WriteLine( "Point did not lie within the Rect" );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }

}
enum Bool
{
   False = 0,
   True
};
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
   public int x;
   public int y;
}

[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
   [FieldOffset(0)] public int left;
   [FieldOffset(4)] public int top;
   [FieldOffset(8)] public int right;
   [FieldOffset(12)] public int bottom;
}

internal static class NativeMethods
{
   [DllImport("user32.dll", CallingConvention=CallingConvention.StdCall)]
   internal static extern Bool PtInRect(ref Rect r, Point p);
};

class TestApplication
{
   public static void Main()
   {
      try
      {
         Bool bPointInRect = 0;
         Rect myRect = new Rect();
         myRect.left = 10;
         myRect.right = 100;
         myRect.top = 10;
         myRect.bottom = 100;
         Point myPoint = new Point();
         myPoint.x = 50;
         myPoint.y = 50;
         bPointInRect = NativeMethods.PtInRect(ref myRect, myPoint);
         if(bPointInRect == Bool.True)
            Console.WriteLine("Point lies within the Rect");
         else
            Console.WriteLine("Point did not lie within the Rect");
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}
'  The program shows a managed declaration of the PtInRect function and defines Point
'  structure with sequential layout and Rect structure with explicit layout. The PtInRect
'  checks the point lies within the rectangle or not.
Imports System.Runtime.InteropServices

   Enum Bool
      [False] = 0
      [True]
   End Enum 
   <StructLayout(LayoutKind.Sequential)>  _
   Public Structure Point
      Public x As Integer
      Public y As Integer
   End Structure 
   
   <StructLayout(LayoutKind.Explicit)>  _   
   Public Structure Rect
      <FieldOffset(0)> Public left As Integer
      <FieldOffset(4)> Public top As Integer
      <FieldOffset(8)> Public right As Integer
      <FieldOffset(12)> Public bottom As Integer
   End Structure 
   
   
   Friend Class NativeMethods
      
      <DllImport("user32.dll", CallingConvention := CallingConvention.StdCall)>  _
      Friend Shared Function PtInRect(ByRef r As Rect, p As Point) As Bool
      End Function	
   End Class
   
   
   Class TestApplication
      
      Public Shared Sub Main()
         Try
            Dim bPointInRect As Bool = 0
            Dim myRect As New Rect()
            myRect.left = 10
            myRect.right = 100
            myRect.top = 10
            myRect.bottom = 100
            Dim myPoint As New Point()
            myPoint.x = 50
            myPoint.y = 50
            bPointInRect = NativeMethods.PtInRect(myRect, myPoint)
            If bPointInRect = Bool.True Then
               Console.WriteLine("Point lies within the Rect")
            Else
               Console.WriteLine("Point did not lie within the Rect")
            End If
         Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message.ToString()))
         End Try
      End Sub 
   End Class

Remarques

Cette énumération est utilisée avec StructLayoutAttribute. Le Common Language Runtime utilise la valeur de Auto disposition par défaut. Pour réduire les problèmes liés à la disposition associés à la Auto valeur, les compilateurs C#, Visual Basic et C++ spécifient Sequential la disposition pour les types valeur.

Important

Le StructLayoutAttribute.Pack champ contrôle l’alignement des champs de données et affecte donc la disposition, quelle que soit la LayoutKind valeur que vous spécifiez. Par défaut, la valeur de Pack est 0, ce qui indique la taille d’empaquetage par défaut pour la plateforme actuelle. Par exemple, lorsque vous utilisez la Explicit valeur de disposition et que vous spécifiez des alignements de champs sur des limites d’octets, vous devez définir Pack sur 1 pour obtenir le résultat souhaité.

S’applique à