See Also: StructLayoutAttribute Members
System.Object
System.Attribute
System.Runtime.InteropServices.StructLayoutAttribute
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
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 System.Runtime.InteropServices.StructLayoutAttribute . Such control is necessary if the unmanaged code expects a specific layout, packing size, or character set.
Note: See the System.Runtime.InteropServices.LayoutKind enumeration for a description of the possible layout schemes, and the System.Runtime.InteropServices.FieldOffsetAttribute 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 System.Runtime.InteropServices.StructLayoutAttribute, and the System.Runtime.InteropServices.FieldOffsetAttribute.
Note: The non-standard PtInRect function used in this example indicates whether the specified point is located inside the specified rectangle. In this example, the layout setting on the Rect structure can be set to LayoutKind.Sequential with no bearing on the end result.C# Example
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.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0