LayoutKind 枚举

定义

当将对象导出到非托管代码时,控制该对象的布局。

C#
public enum LayoutKind
C#
[System.Serializable]
public enum LayoutKind
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum LayoutKind
继承
LayoutKind
属性

字段

名称 说明
Auto 3

运行库自动为非托管内存中的对象的成员选择适当的布局。 使用此枚举成员定义的对象不能在托管代码的外部公开。 尝试这样做将引发异常。

Explicit 2

在未管理内存中的每一个对象成员的精确位置是被显式控制的,服从于 Pack 字段的设置。 每个成员必须使用 FieldOffsetAttribute 指示该字段在类型中的位置。

Sequential 0

对象的成员按照它们在被导出到非托管内存时出现的顺序依次布局。 这些成员根据在 Pack 中指定的封装进行布局,并且可以是不连续的。

示例

以下示例演示 函数的 PtInRect 托管声明,该函数检查点是否位于矩形内,并定义 Point 具有顺序布局的结构和 Rect 具有显式布局的结构。

C#
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);
      }
   }
}

注解

此枚举与 一 StructLayoutAttribute起使用。 默认情况下,公共语言运行时使用 Auto 布局值。 为了减少与值相关的 Auto 布局相关问题,C#、Visual Basic 和 C++ 编译器为值类型指定 Sequential 布局。

重要

字段 StructLayoutAttribute.Pack 控制数据字段的对齐方式,因此无论指定的值如何 LayoutKind ,都会影响布局。 默认情况下, 的 Pack 值为 0,表示当前平台的默认打包大小。 例如,使用 Explicit 布局值并在字节边界上指定字段对齐时,必须将 设置为 Pack 1 才能获得所需的结果。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0