RuntimeInfrastructure mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.Attribute System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.All, Inherited=false) System.Runtime.InteropServices.ComVisible(true)

The allows the user to control the physical layout of the data members of a class or structure.

The target objects for this attribute are classes and structures. By default, the physical layout of the data members of a target object is automatically arranged. When managed objects are passed as arguments to unmanaged code, the system creates their unmanaged representations. These unmanaged representations can be controlled with the . Such control is necessary if the unmanaged code expects a specific layout, packing size, or character set.

See the enumeration for a description of the possible layout schemes, and the for further information on the layout of exported objects.

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

The following example demonstrates the use of the , and the .

The non-standard function used in this example indicates whether the specified point is located inside the specified rectangle. In this example, the layout setting on the structure can be set to with no bearing on the end result.

`using System; using System.Runtime.InteropServices; [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; } class NativeCodeAPI { [DllImport("User32.dll")] public static extern bool PtInRect(ref Rect r, Point p); } public class StructLayoutTest { public static void Main() { Rect r; Point p1, p2; r.left = 0; r.right = 100; r.top = 0; r.bottom = 100; p1.x = 20; p1.y = 30; p2.x = 110; p2.y = 5; bool isInside1 = NativeCodeAPI.PtInRect(ref r, p1); bool isInside2 = NativeCodeAPI.PtInRect(ref r, p2); if(isInside1) Console.WriteLine("The first point is inside the rectangle."); else Console.WriteLine("The first point is outside the rectangle."); if(isInside2) Console.WriteLine("The second point is inside the rectangle."); else Console.WriteLine("The second point is outside the rectangle."); } }`

The output is

The first point is inside the rectangle.

The second point is outside the rectangle.

- Constructor
  A set to a value that specifies how the class or structure is arranged in memory.

  Constructs and initializes a new instance of the class with the specified value.

  If the parameter does not represent a valid value, a runtime error occurs.

  0
  1.0.5000.0
  2.0.0.0
- Constructor
  A value that specifies how the class or structure is arranged in memory.

  Constructs and initializes a new instance of the class with the specified value.

  If contains an invalid value, a runtime error occurs.

  0
  1.0.5000.0
  2.0.0.0
- Field
  System.Runtime.InteropServices.CharSet

  A value that indicates the character set in which strings of an object are marshaled.

  See the enumeration for a description of different character sets.

  The default value of this field is .

  0
  1.0.5000.0
  2.0.0.0
- Field
  System.Int32

  A that indicates the packing alignment used with the layout.

  The field determines memory alignment of data fields of a target object.

  Data fields of a target object exported to unmanaged memory are aligned on a byte boundary that is a multiple of bytes, or at some natural alignment for that field type, whichever is smaller.

  The value of is required to be 0, 1, 2, 4, 8, 16, 32, 64, or 128. A value of zero indicates that the packing alignment is set to the default for the current platform. The default value is implementation-defined.

  0
  1.0.5000.0
  2.0.0.0
- Field
  System.Int32

  A that indicates the size of the memory block to be allocated for an instance of the type qualified by the current .

  is required to be zero, or greater than or equal to the calculated size of the target object, based on the field indicating the packing alignment. A of zero indicates that the size is calculated from the field types, their specified offsets, the packing size (default or specified) and natural alignment on the target, runtime platform.

  For additional information on the field, see Partition II of the CLI Specification.

  0
  1.0.5000.0
  2.0.0.0
- Property
  System.Runtime.InteropServices.LayoutKind

  Gets the value that specifies how the target object is arranged.

  A value that specifies how the target object is arranged.

  This property is read-only.

  0
  1.0.5000.0
  2.0.0.0
0