Share via


StructLayoutAttribute クラス

StructLayoutAttribute クラスを使用すると、クラスまたは構造体のデータ フィールドの物理的なレイアウトを制御できます。

この型のすべてのメンバの一覧については、StructLayoutAttribute メンバ を参照してください。

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

<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Struct)>
NotInheritable Public Class StructLayoutAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public sealed class StructLayoutAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Class |
   AttributeTargets::Struct)]
public __gc __sealed class StructLayoutAttribute : public   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)
class StructLayoutAttribute extends Attribute

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

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

一般に、共通言語ランタイムがマネージ メモリ内のクラスまたは構造体のデータ フィールドの物理的なレイアウトを制御します。クラスまたは構造体を特定の方法で整列する必要がある場合は、 StructLayoutAttribute を使用します。特定のレイアウトを予期するアンマネージ コードにクラスを渡す場合、クラス レイアウトの明示的な制御は重要です。 LayoutKind 値の Sequential は、表示する順序に従ってメンバをレイアウトするために使用します。 Explicit は、各データ メンバの正確な位置を制御します。 Explicit では、各メンバは FieldOffsetAttribute を使用して、その型内でフィールドの位置を指定する必要があります。

C#、Visual Basic .NET、および C++ のコンパイラは、既定で Sequential レイアウト値をクラスと構造体に適用します。 タイプ ライブラリ インポータ (Tlbimp.exe) もこの属性を適用します。タイプ ライブラリをインポートするときに常に Sequential 値を適用します。

使用例

[Visual Basic, C#, C++] 次に示すのは、 GetSystemTime 関数のマネージ宣言の例です。このコードは、 MySystemTime クラスを LayoutKind.Explicit レイアウトで定義します。また、 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 

[C#] 
[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);
      }
   }
}

[C++] 

[StructLayout(LayoutKind::Explicit, Size=16, CharSet=CharSet::Ansi)]
__value class MySystemTime {
public:
   [FieldOffset(0)] short int wYear;
   [FieldOffset(2)] short int  wMonth;
   [FieldOffset(4)] short int  wDayOfWeek;
   [FieldOffset(6)] short int  wDay;
   [FieldOffset(8)] short int  wHour;
   [FieldOffset(10)] short int  wMinute;
   [FieldOffset(12)] short int  wSecond;
   [FieldOffset(14)] short int  wMilliseconds;
};

__gc  class LibWrapper {
public:
   [DllImport(S"kernel32.dll")]
   static void GetSystemTime(MySystemTime*  st);
};

int main() {
   try {
      MySystemTime sysTime;
      LibWrapper::GetSystemTime(&sysTime);
      Console::WriteLine(S"The System time is {0}/{1}/{2} {3}:{4}:{5}",
         __box(sysTime.wDay), __box(sysTime.wMonth), __box(sysTime.wYear),
         __box(sysTime.wHour), sysTime.wMinute, sysTime.wSecond);
   } catch (TypeLoadException* e) {
      Console::WriteLine(S"TypeLoadException : {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception : {0}", e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Runtime.InteropServices

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

StructLayoutAttribute メンバ | System.Runtime.InteropServices 名前空間 | タイプ ライブラリ インポータ (Tlbimp.exe)