Type.IsLayoutSequential 属性

定义

获取指示当前类型的字段是否按顺序(定义顺序或发送到元数据的顺序)放置的值。

C#
public bool IsLayoutSequential { get; }

属性值

Boolean

如果当前类型的 true 属性包括 Attributes,则为 SequentialLayout;否则为 false

实现

示例

以下示例创建类的实例,该类中已设置 类中的枚举值,检查 属性并显示 LayoutKind.Sequential StructLayoutAttribute IsLayoutSequential 结果。

C#
using System;
using System.Reflection;
using System.ComponentModel;
using System.Runtime.InteropServices;
class MyTypeSequential1
{
}
[StructLayoutAttribute(LayoutKind.Sequential)]
class MyTypeSequential2
{
    public static void Main(string []args)
    {
        try
        {
            // Create an instance of myTypeSeq1.
            MyTypeSequential1 myObj1 = new MyTypeSequential1();
            Type myTypeObj1 = myObj1.GetType();
            // Check for and display the SequentialLayout attribute.
            Console.WriteLine("\nThe object myObj1 has IsLayoutSequential: {0}.", myObj1.GetType().IsLayoutSequential);
            // Create an instance of 'myTypeSeq2' class.
            MyTypeSequential2 myObj2 = new MyTypeSequential2();
            Type myTypeObj2 = myObj2.GetType();
            // Check for and display the SequentialLayout attribute.
            Console.WriteLine("\nThe object myObj2 has IsLayoutSequential: {0}.", myObj2.GetType().IsLayoutSequential);
        }
        catch(Exception e)
        {
            Console.WriteLine("\nAn exception occurred: {0}", e.Message);
        }
    }
}

注解

为方便起见,提供了此属性。 或者,可以使用 TypeAttributes.LayoutMask 枚举值选择类型布局属性,然后测试是否 TypeAttributes.SequentialLayout 设置了 。 、 TypeAttributes.AutoLayout TypeAttributes.ExplicitLayoutTypeAttributes.SequentialLayout 枚举值指示类型字段在内存中的布局方式。

对于动态类型,可以在 TypeAttributes.SequentialLayout 创建类型时指定 。 在代码中,将具有 枚举值的属性应用于 类型, StructLayoutAttribute LayoutKind.Sequential 以指定布局是按顺序的。

备注

不能使用 方法 GetCustomAttributes 来确定 是否已 StructLayoutAttribute 应用于类型。

有关详细信息,请参阅公共语言基础结构 (CLI) 文档规范的第 9.1.2 节:"分区 II:元数据定义和语义"。 可联机获取该文档;请参阅 MSDN 上的 ECMA C# 和公共语言基础结构标准和 Ecma International 网站上的标准 ECMA-335 - 公共语言基础结构 (CLI)

如果当前 表示构造的泛型类型,则此属性将应用于构造该类型的 Type 泛型类型定义。 例如,如果当前 表示 (Type MyGenericType<int> MyGenericType(Of Integer) 中Visual Basic) ,则此属性的值由 确定 MyGenericType<T>

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

另请参阅