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 MySystemTime layout を使用してクラスを定義し LayoutKind.Explicit ます。 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)

適用対象

こちらもご覧ください