英語で読む

次の方法で共有


Type.IsExplicitLayout プロパティ

定義

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

C#
public bool IsExplicitLayout { get; }

プロパティ値

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

実装

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

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

注釈

便利なプロパティです。 または、 列挙値を 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

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください