StructLayoutAttribute 類別

定義

讓您控制記憶體中類別或結構之資料欄位的實際配置。

public ref class StructLayoutAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)]
public sealed class StructLayoutAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class StructLayoutAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)>]
type StructLayoutAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StructLayoutAttribute = class
    inherit Attribute
Public NotInheritable Class StructLayoutAttribute
Inherits Attribute
繼承
StructLayoutAttribute
屬性

範例

下列範例示範函式的 Managed 宣告, GetSystemTime 並使用配置定義 MySystemTime 類別 LayoutKind.ExplicitGetSystemTime 取得系統時間,並列印至主控台。

using namespace System;
using namespace System::Runtime::InteropServices;


[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 NativeMethods
{
public:

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

int main()
{
   try
   {
      MySystemTime sysTime;
      NativeMethods::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 );
   }

}
using System;
using System.Runtime.InteropServices;

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;
   }

   internal static class NativeMethods
   {
      [DllImport("kernel32.dll")]
      internal static extern void GetSystemTime([MarshalAs(UnmanagedType.LPStruct)]MySystemTime st);
   };

   class TestApplication
   {
      public static void Main()
      {
         try
         {
            MySystemTime sysTime = new MySystemTime();
            NativeMethods.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);
         }
      }
   }
}
Imports System.Runtime.InteropServices

Namespace InteropSample

   <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


   Friend Class NativeMethods

      <DllImport("kernel32.dll")> _
      Friend Shared Sub GetSystemTime(<MarshalAs(UnmanagedType.LPStruct)> ByVal st As MySystemTime)
      End Sub
   End Class

   Class TestApplication

      Public Shared Sub Main()
         Try
            Dim sysTime As New MySystemTime()
            NativeMethods.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
   End Class
End Namespace 'InteropSample

備註

您可以將此屬性套用至類別或結構。

Common Language Runtime 會控制 Managed 記憶體中類別或結構之資料欄位的實體配置。 不過,如果您想要將類型傳遞至 Unmanaged 程式碼,您可以使用 StructLayoutAttribute 屬性來控制類型的 Unmanaged 配置。 使用 屬性搭配 LayoutKind.Sequential ,強制依成員出現的順序循序配置。 針對 Blittable 類型, LayoutKind.Sequential 控制 Managed 記憶體中的配置,以及 Unmanaged 記憶體中的配置。 針對非 Blittable 類型,它會控制類別或結構封送處理至 Unmanaged 程式碼時的配置,但不會控制 Managed 記憶體中的配置。 使用 屬性搭配 LayoutKind.Explicit 來控制每個資料成員的精確位置。 這會影響 blittable 和非受控類型的 Managed 和 Unmanaged 版面配置。 使用 LayoutKind.Explicit 需要您使用 FieldOffsetAttribute 屬性來指出類型中每個欄位的位置。

C#、Visual Basic 和 C++ 編譯器預設會將 Sequential 配置值套用至結構。 對於類別,您必須明確套用 LayoutKind.Sequential 值。 Tlbimp.exe (型別程式庫匯入工具) 也會套用 StructLayoutAttribute 屬性;它一律會在匯入型別程式庫時套用 LayoutKind.Sequential 值。

建構函式

StructLayoutAttribute(Int16)

使用指定的 StructLayoutAttribute 列舉型別 (Enumeration) 成員,初始化 LayoutKind 類別的新執行個體。

StructLayoutAttribute(LayoutKind)

使用指定的 StructLayoutAttribute 列舉型別 (Enumeration) 成員,初始化 LayoutKind 類別的新執行個體。

欄位

CharSet

表示根據預設,類別中的字串資料欄位應該封送處理為 LPWSTR,還是 LPSTR

Pack

控制記憶體中類別或結構之資料欄位的對齊。

Size

指示結構或類別的絕對大小。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得 LayoutKind 值,指定類別或結構的排列方式。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱