Type.IsAutoLayout 属性

定义

获取指示当前类型的字段是否由公共语言运行时自动放置的值。

C#
public bool IsAutoLayout { get; }

属性值

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

实现

示例

以下示例创建 类型的实例并显示 属性 IsAutoLayout

C#
using System;
using System.Runtime.InteropServices;

// The Demo class is attributed as AutoLayout.
[StructLayoutAttribute(LayoutKind.Auto)]
public class Demo
{
}

public class Example
{
    public static void Main()
    {
        // Create an instance of the Type class using the GetType method.
        Type  myType=typeof(Demo);
        // Get and display the IsAutoLayout property of the
        // Demoinstance.
        Console.WriteLine("\nThe AutoLayout property for the Demo class is {0}.",
            myType.IsAutoLayout);
    }
}

注解

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

对于动态类型,可以在创建类型时指定 TypeAttributes.AutoLayout 。 在代码中,将 StructLayoutAttribute 具有 LayoutKind.Auto 枚举值的特性应用于类型,以便运行时确定布局类的适当方式。

备注

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

如果当前 Type 表示构造的泛型类型,则此属性适用于从中构造该类型的泛型类型定义。 例如,如果当前 Type 表示 MyGenericType<int> Visual Basic) 中的 (MyGenericType(Of Integer) ,则此属性的值由 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

另请参阅