StructLayoutAttribute 类

StructLayoutAttribute 类使用户可以控制类或结构的数据字段的物理布局。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct, Inherited:=False)> _
Public NotInheritable Class StructLayoutAttribute
    Inherits Attribute
用法
Dim instance As StructLayoutAttribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false)] 
public sealed class StructLayoutAttribute : Attribute
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct, Inherited=false)] 
public ref class StructLayoutAttribute sealed : public Attribute
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false) */ 
public final class StructLayoutAttribute extends Attribute
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct, Inherited=false) 
public final class StructLayoutAttribute extends Attribute

备注

可将该属性应用于类或结构。

通常,公共语言运行库控制类或结构的数据字段在托管内存中的物理布局。如果类或结构需要按某种方式排列,则可以使用 StructLayoutAttribute。如果要将类传递给需要指定布局的非托管代码,则显式控制类布局是重要的。LayoutKindSequential 用于强制将成员按其出现的顺序进行顺序布局。Explicit 控制每个数据成员的精确位置。如果使用 Explicit,则每个成员必须使用 FieldOffsetAttribute 来指示该字段在类型中的位置。

默认情况下,C#、Visual Basic .默认情况下,NET 和 C++ 编译器将 Sequential 布局值应用于结构。对于类,必须显式应用 Sequential 值。类型库导入程序 (Tlbimp.exe) 也应用此属性;它总是在导入类型库时应用 Sequential 值。

示例

下面的示例演示 GetSystemTime 函数的托管声明,并定义具有 LayoutKind.Explicit 布局的 MySystemTime 类。GetSystemTime 获取系统时间并输出到控制台。

   <StructLayout(LayoutKind.Explicit, Size := 16, CharSet := CharSet.Ansi)>  _
   Public Class MySystemTime
      <FieldOffset(0)> Public wYear As Short
      <FieldOffset(2)> Public wMonth As Short
      <FieldOffset(4)> Public wDayOfWeek As Short
      <FieldOffset(6)> Public wDay As Short
      <FieldOffset(8)> Public wHour As Short
      <FieldOffset(10)> Public wMinute As Short
      <FieldOffset(12)> Public wSecond As Short
      <FieldOffset(14)> Public wMilliseconds As Short
   End Class 'MySystemTime

   
   Class LibWrapper
      
      <DllImport("kernel32.dll")>  _
      Public Shared Sub GetSystemTime(<MarshalAs(UnmanagedType.LPStruct)> st As MySystemTime)
      End SUb   
   End Class 'LibWrapper

   Class TestApplication
      
      Public Shared Sub Main()
         Try
            Dim sysTime As 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 e As TypeLoadException
            Console.WriteLine(("TypeLoadException : " + e.Message.ToString()))
         Catch e As Exception
            Console.WriteLine(("Exception : " + e.Message.ToString()))
         End Try
      End Sub 'Main
   End Class 'TestApplication
End Namespace 'InteropSample 
[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);
      }
   }
}
[StructLayout(LayoutKind::Explicit,Size=16,CharSet=CharSet::Ansi)]
value class MySystemTime
{
public:

   [FieldOffset(0)]
   short wYear;

   [FieldOffset(2)]
   short wMonth;

   [FieldOffset(4)]
   short wDayOfWeek;

   [FieldOffset(6)]
   short wDay;

   [FieldOffset(8)]
   short wHour;

   [FieldOffset(10)]
   short wMinute;

   [FieldOffset(12)]
   short wSecond;

   [FieldOffset(14)]
   short wMilliseconds;
};

ref class LibWrapper
{
public:

   [DllImport("kernel32.dll")]
   static void GetSystemTime( MySystemTime * st );
};

int main()
{
   try
   {
      MySystemTime sysTime;
      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 : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0}", e->Message );
   }

}
/** @attribute StructLayout(LayoutKind.Explicit, Size = 16, 
    CharSet = CharSet.Ansi)
 */
public class MySystemTime
{
    /** @attribute FieldOffset(0)
     */
    public short wYear;
    
    /** @attribute FieldOffset(2)
     */
    public short wMonth;
    
    /** @attribute FieldOffset(4)
     */
    public short wDayOfWeek;

    /** @attribute FieldOffset(6)
     */
    public short wDay;

    /** @attribute FieldOffset(8)
     */
    public short wHour;

    /** @attribute FieldOffset(10)
     */
    public short wMinute;

    /** @attribute FieldOffset(12)
     */
    public short wSecond;

    /** @attribute FieldOffset(14)
     */
    public short wMilliseconds;

} //MySystemTime

class LibWrapper
{
    /** @attribute DllImport("kernel32.dll")
     */
    public static native void GetSystemTime(
        /** @attribute MarshalAs(UnmanagedType.LPStruct)
         */
        MySystemTime st);
} //LibWrapper

class TestApplication
{
    public static void main(String[] args)
    {
        try {
            MySystemTime sysTime = new MySystemTime();
            LibWrapper.GetSystemTime(sysTime);
            Console.WriteLine("The System time is {0}/{1}/{2} {3}:{4}:{5}",
                new Object[] { (Int32)sysTime.wDay, (Int32)sysTime.wMonth,
                (Int32)sysTime.wYear, (Int32)sysTime.wHour, (Int32)sysTime.
                wMinute, (Int32)sysTime.wSecond });
        }
        catch (TypeLoadException e) {
            Console.WriteLine("TypeLoadException : " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : " + e.get_Message());
        }
    } //main
} //TestApplication

继承层次结构

System.Object
   System.Attribute
    System.Runtime.InteropServices.StructLayoutAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

StructLayoutAttribute 成员
System.Runtime.InteropServices 命名空间

其他资源

类型库导入程序 (Tlbimp.exe)