Type.IsLayoutSequential 属性

定义

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

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

属性值

Boolean

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

实现

示例

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

#using <System.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
ref class MyTypeSequential1{};


[StructLayoutAttribute(LayoutKind::Sequential)]
ref class MyTypeSequential2{};

int main()
{
   try
   {
      
      // Create an instance of myTypeSeq1.
      MyTypeSequential1^ myObj1 = gcnew MyTypeSequential1;
      
      // 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 = gcnew MyTypeSequential2;
      
      // 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 );
   }

}
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);
        }
    }
}
Imports System.Reflection
Imports System.ComponentModel
Imports System.Runtime.InteropServices

Class MyTypeSequential1
End Class
<StructLayoutAttribute(LayoutKind.Sequential)> Class MyTypeSequential2
    Public Shared Sub Main()
        Try
            ' Create an instance of MyTypeSequential1.
            Dim myObj1 As New MyTypeSequential1()
            Dim myTypeObj1 As Type = myObj1.GetType()
            ' Check for and display the SequentialLayout attribute.
            Console.WriteLine(ControlChars.Cr + "The object myObj1 has IsLayoutSequential: {0}.", myObj1.GetType().IsLayoutSequential.ToString())
            ' Create an instance of MyTypeSequential2.
            Dim myObj2 As New MyTypeSequential2()
            Dim myTypeObj2 As Type = myObj2.GetType()
            ' Check for and display the SequentialLayout attribute.
            Console.WriteLine(ControlChars.Cr + "The object myObj2 has IsLayoutSequential: {0}.", myObj2.GetType().IsLayoutSequential.ToString())
        Catch e As Exception
            Console.WriteLine(ControlChars.Cr + "An exception occurred: {0}", e.Message.ToString())
        End Try
    End Sub
End Class

注解

为方便起见,提供了此属性。 或者,可以使用 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

适用于

另请参阅