LayoutKind 枚举

定义

控制当导出到非托管代码时对象的布局。Controls the layout of an object when exported to unmanaged code.

public enum class LayoutKind
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum LayoutKind
type LayoutKind = 
Public Enum LayoutKind
继承
LayoutKind
属性

字段

Auto 3

运行库自动为非托管内存中的对象的成员选择适当的布局。The runtime automatically chooses an appropriate layout for the members of an object in unmanaged memory. 使用此枚举成员定义的对象不能在托管代码的外部公开。Objects defined with this enumeration member cannot be exposed outside of managed code. 尝试这样做将引发异常。Attempting to do so generates an exception.

Explicit 2

在未管理内存中的每一个对象成员的精确位置是被显式控制的,服从于 Pack 字段的设置。The precise position of each member of an object in unmanaged memory is explicitly controlled, subject to the setting of the Pack field. 每个成员必须使用 FieldOffsetAttribute 指示该字段在类型中的位置。Each member must use the FieldOffsetAttribute to indicate the position of that field within the type.

Sequential 0

对象的成员按照它们在被导出到非托管内存时出现的顺序依次布局。The members of the object are laid out sequentially, in the order in which they appear when exported to unmanaged memory. 这些成员根据在 Pack 中指定的封装进行布局,并且可以是不连续的。The members are laid out according to the packing specified in Pack, and can be noncontiguous.

示例

下面的示例演示PtInRect函数的托管声明, 该声明检查某个点是否位于矩形内, 并Point定义Rect具有顺序布局的结构以及具有显式布局的结构。The following example shows the managed declaration of the PtInRect function, which checks whether a point lies within a rectangle, and defines a Point structure with Sequential layout and a Rect structure with Explicit layout.

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 

注解

此枚举与StructLayoutAttribute一起使用。This enumeration is used with StructLayoutAttribute. 默认情况下, 公共语言Auto运行时使用布局值。The common language runtime uses the Auto layout value by default. 若要Auto减少与值、、Visual Basic 和C++编译器关联C#的与布局相关的问题Sequential , 请指定值类型的布局。To reduce layout-related problems associated with the Auto value, C#, Visual Basic, and C++ compilers specify Sequential layout for value types.

重要

StructLayoutAttribute.Pack字段控制数据字段的对齐, 从而影响布局, 而不考虑LayoutKind指定的值。The StructLayoutAttribute.Pack field controls the alignment of data fields, and thus affects the layout regardless of the LayoutKind value you specify. 默认情况下, 的Pack值为 0, 指示当前平台的默认包装大小。By default, the value of Pack is 0, which indicates the default packing size for the current platform. 例如, 使用Explicit布局值并在字节边界上指定字段对齐时, 必须将设置Pack为1才能获得所需的结果。For example, when you use the Explicit layout value and specify field alignments on byte boundaries, you must set Pack to 1 to get the desired result.

适用于