Type.IsExplicitLayout プロパティ

定義

現在の型のフィールドが、明示的に指定したオフセット位置に配置されているかどうかを示す値を取得します。

public:
 property bool IsExplicitLayout { bool get(); };
public bool IsExplicitLayout { get; }
member this.IsExplicitLayout : bool
Public ReadOnly Property IsExplicitLayout As Boolean

プロパティ値

現在の型の true プロパティに Attributes が含まれる場合は ExplicitLayout。それ以外の場合は false

実装

次の例では、 型のインスタンスを作成し、その IsExplicitLayout プロパティの値を表示します。 クラスを MySystemTime 使用します。これは、 のコード例にも含まれています StructLayoutAttribute

using System;
using System.Reflection;
using System.ComponentModel;
using System.Runtime.InteropServices;

// Class to test for the ExplicitLayout property.
[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;
}

public class Program
{
    public static void Main(string[] args)
    {
        // Create an instance of the type using the GetType method.
        Type  t = typeof(MySystemTime);
        // Get and display the IsExplicitLayout property.
        Console.WriteLine("\nIsExplicitLayout for MySystemTime is {0}.",
            t.IsExplicitLayout);
    }
}
open System.Runtime.InteropServices

// Class to test for the ExplicitLayout property.
[<StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)>]
type MySystemTime =
   [<FieldOffset 0>] val public wYear: uint16
   [<FieldOffset 2>] val public wMonth: uint16
   [<FieldOffset 4>] val public wDayOfWeek: uint16
   [<FieldOffset 6>] val public wDay: uint16
   [<FieldOffset 8>] val public wHour: uint16
   [<FieldOffset 10>] val public wMinute: uint16
   [<FieldOffset 12>] val public wSecond: uint16
   [<FieldOffset 14>] val public wMilliseconds: uint16

// Create an instance of the type using the GetType method.
let t = typeof<MySystemTime>
// Get and display the IsExplicitLayout property.
printfn $"\nIsExplicitLayout for MySystemTime is {t.IsExplicitLayout}."
Imports System.Reflection
Imports System.ComponentModel
Imports System.Runtime.InteropServices

'Class to test for the ExplicitLayout property.
   <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 

Public Class Program
    Public Shared Sub Main()
        'Create an instance of type using the GetType method.
        Dim t As Type = GetType(MySystemTime)
        ' Get and display the IsExplicitLayout property.
        Console.WriteLine(vbCrLf & "IsExplicitLayout for MySystemTime is {0}.", _
            t.IsExplicitLayout)
    End Sub
End Class

注釈

便利なプロパティです。 または、 列挙値を TypeAttributes.LayoutMask 使用して型レイアウト属性を選択し、 が設定されているかどうかを TypeAttributes.ExplicitLayout テストすることもできます。 、TypeAttributes.ExplicitLayout、および TypeAttributes.SequentialLayout 列挙値はTypeAttributes.AutoLayout、型のフィールドがメモリ内に配置される方法を示します。

動的型の場合は、型を作成するタイミングを指定 TypeAttributes.ExplicitLayout できます。 コードでは、列挙型の値を StructLayoutAttribute 持つ 属性を LayoutKind.Explicit 型に適用して、フィールドの開始位置のオフセットを明示的に指定するように指定します。

注意

メソッドを使用して、 GetCustomAttributes が型に適用されているかどうかを判断 StructLayoutAttribute することはできません。

現在 Type の が構築されたジェネリック型を表す場合、このプロパティは、型の構築元のジェネリック型定義に適用されます。 たとえば、現在Typeの が (Visual Basic では )MyGenericType(Of Integer) を表すMyGenericType<int>場合、このプロパティの値は によってMyGenericType<T>決定されます。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このプロパティは常に を返します false

適用対象

こちらもご覧ください