public sealed class StructLayoutAttribute : Attribute
Object
Attribute
StructLayoutAttribute
mscorlib
RuntimeInfrastructure
The StructLayoutAttribute 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 StructLayoutAttribute . Such control is necessary if the unmanaged code expects a specific layout, packing size, or character set.[Note: See the LayoutKind enumeration for a description of the possible layout schemes, and the 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 StructLayoutAttribute, and the 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 theRect
structure can be set to System.Runtime.InteropServices.LayoutKind.Sequential 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.
AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple=false, Inherited=false)
System.Runtime.InteropServices Namespace
StructLayoutAttribute Constructors
StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind) Constructor
StructLayoutAttribute(short) Constructor
StructLayoutAttribute Fields
StructLayoutAttribute.CharSet Field
StructLayoutAttribute.Pack Field
StructLayoutAttribute.Size Field
StructLayoutAttribute Properties
public StructLayoutAttribute(LayoutKind layoutKind);
Constructs and initializes a new instance of the StructLayoutAttribute class with the specified LayoutKind value.
- layoutKind
- A LayoutKind value that specifies how the class or structure is arranged in memory.
If layoutKind contains an invalid LayoutKind value, a runtime error occurs.
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace
public StructLayoutAttribute(short layoutKind);
Constructs and initializes a new instance of the StructLayoutAttribute class with the specified value.
- layoutKind
- A Int16 set to a LayoutKind value that specifies how the class or structure is arranged in memory.
If the layoutKind parameter does not represent a valid LayoutKind value, a runtime error occurs.
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace
public CharSet CharSet;
A CharSet value that indicates the character set in which strings of an object are marshaled.
[Note: See the CharSet enumeration for a description of different character sets.]
The default value of this field is System.Runtime.InteropServices.CharSet.Ansi.
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace
public int Pack;
A Int32 that indicates the packing alignment used with the System.Runtime.InteropServices.LayoutKind.Sequential layout.
The System.Runtime.InteropServices.StructLayoutAttribute.Pack 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 System.Runtime.InteropServices.StructLayoutAttribute.Pack bytes, or at some natural alignment for that field type, whichever is smaller.
The value of System.Runtime.InteropServices.StructLayoutAttribute.Pack 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.
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace
public int Size;
A Int32 that indicates the size of the memory block to be allocated for an instance of the type qualified by the current StructLayoutAttribute.
System.Runtime.InteropServices.StructLayoutAttribute.Size is required to be zero, or greater than or equal to the calculated size of the target object, based on the System.Runtime.InteropServices.StructLayoutAttribute.Pack field indicating the packing alignment. A System.Runtime.InteropServices.StructLayoutAttribute.Size 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.[Note: For additional information on the System.Runtime.InteropServices.StructLayoutAttribute.Size field, see Partition II of the CLI Specification.]
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace
public LayoutKind Value { get; }
Gets the LayoutKind value that specifies how the target object is arranged.
A LayoutKind value that specifies how the target object is arranged.
This property is read-only.
System.Runtime.InteropServices.StructLayoutAttribute Class, System.Runtime.InteropServices Namespace