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
属性

次の例では、GetSystemTime関数のマネージド宣言を示し、LayoutKind.Explicit レイアウトMySystemTimeクラスを定義します。 GetSystemTime はシステム時刻を取得し、コンソールに出力します。

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

注釈

この属性は、クラスまたは構造体に適用できます。

共通言語ランタイムは、マネージド メモリ内のクラスまたは構造体のデータ フィールドの物理的なレイアウトを制御します。 ただし、型をアンマネージ コードに渡す場合は、 StructLayoutAttribute 属性を使用して、型のアンマネージ レイアウトを制御できます。 LayoutKind.Sequential属性を使用して、メンバーが表示される順序で順番にレイアウトされるようにします。 blittable 型の場合、 LayoutKind.Sequential は、マネージド メモリ内のレイアウトとアンマネージ メモリ内のレイアウトの両方を制御します。 blittable 以外の型の場合、クラスまたは構造体がアンマネージ コードにマーシャリングされるときにレイアウトを制御しますが、マネージド メモリ内のレイアウトは制御しません。 各データ メンバーの正確な位置を制御するには、属性と LayoutKind.Explicit を使用します。 これは、blittable 型と非 blittable 型の両方について、マネージド レイアウトとアンマネージド レイアウトの両方に影響します。 LayoutKind.Explicitを使用するには、FieldOffsetAttribute属性を使用して、型内の各フィールドの位置を示す必要があります。

C#、Visual Basic、および C++ コンパイラは、既定で Sequential レイアウト値を構造体に適用します。 クラスの場合は、 LayoutKind.Sequential 値を明示的に適用する必要があります。 Tlbimp.exe (タイプ ライブラリ インポーター) では、 StructLayoutAttribute 属性も適用されます。タイプ ライブラリをインポートするときに、常に LayoutKind.Sequential 値が適用されます。

コンストラクター

名前 説明
StructLayoutAttribute(Int16)

指定したStructLayoutAttribute列挙メンバーを使用して、LayoutKind クラスの新しいインスタンスを初期化します。

StructLayoutAttribute(LayoutKind)

指定したStructLayoutAttribute列挙メンバーを使用して、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)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

インターフェイスの型情報を取得するために使用できるオブジェクトの型情報を取得します。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください