[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] |
Typically, the common language runtime controls the physical layout of the data fields of a class or structure in managed memory. If the class or structure needs to be arranged a certain way, you can use StructLayoutAttribute. Explicit control of a class layout is important if the class is to be passed to unmanaged code that expects a specific layout. The LayoutKind value Sequential is used to force the members to be laid out sequentially in the order they appear.Explicit is used to control the precise position of each data member. With Explicit, each member must use the FieldOffsetAttribute to indicate the position of that field within the type.
[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)] public class MySystemTime { [FieldOffset(0)]public ushort wYear; [FieldOffset(2)]public ushort wMonth; [FieldOffset(4)]public ushort wDayOfWeek; [FieldOffset(6)]public ushort wDay; [FieldOffset(8)]public ushort wHour; [FieldOffset(10)]public ushort wMinute; [FieldOffset(12)]public ushort wSecond; [FieldOffset(14)]public ushort wMilliseconds; } class LibWrapper { [DllImport("kernel32.dll")] public static extern void GetSystemTime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st); }; class TestApplication { public static void Main() { try { MySystemTime sysTime = new MySystemTime(); LibWrapper.GetSystemTime(sysTime); Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}", sysTime.wDay, sysTime.wMonth, sysTime.wYear, sysTime.wHour, sysTime.wMinute, sysTime.wSecond); } catch(TypeLoadException e) { Console.WriteLine("TypeLoadException : " + e.Message); } catch(Exception e) { Console.WriteLine("Exception : " + e.Message); } } }
ctor #1 | Overloaded:.ctor(short layoutKind) Initalizes a new instance of the StructLayoutAttribute class with the specified LayoutKind value. |
ctor #2 | Overloaded:.ctor(LayoutKind layoutKind) Initalizes a new instance of the StructLayoutAttribute class with the specified LayoutKind value. |
CharSet | Indicates how string data fields within the class should be marshaled. |
Pack | Controls the alignment of data fields of a class or structure in memory. |
Size | Indicates the absolute size of the structure or class. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Value | Read-only Gets the LayoutKind value that specifies how the class or structure is arranged. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public StructLayoutAttribute( |
layoutKind
public StructLayoutAttribute( |
layoutKind
public CharSet CharSet;
|
public int Pack;
|
The default packing size is 8.
public int Size;
|
public virtual object TypeId {get;}
|
public LayoutKind Value {get;}
|
~StructLayoutAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |