語言

Type.IsLayoutSequential 屬性

定義

會得到一個值,表示目前型態欄位是否依定義順序排列,或依照它們被指向元資料的順序排列。

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

屬性值

true 若 Attributes 當前型態的性質包含 SequentialLayout;否則,則 false。

實作

範例

以下範例建立一個類別的實例,該 LayoutKind.Sequential 類別的列舉值 StructLayoutAttribute 已被設定,檢查該 IsLayoutSequential 屬性並顯示結果。

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);
        }
    }
}
open System.Runtime.InteropServices

type MyTypeSequential1 = struct end

[<StructLayoutAttribute(LayoutKind.Sequential)>]
type MyTypeSequential2 = struct end

try
    // Create an instance of myTypeSeq1.
    let myObj1 = MyTypeSequential1()
    let myTypeObj1 = myObj1.GetType()
    // Check for and display the SequentialLayout attribute.
    printfn $"\nThe object myObj1 has IsLayoutSequential: {myTypeObj1.IsLayoutSequential}."
    // Create an instance of 'myTypeSeq2' class.
    let myObj2 = MyTypeSequential2()
    let myTypeObj2 = myObj2.GetType()
    // Check for and display the SequentialLayout attribute.
    printfn $"\nThe object myObj2 has IsLayoutSequential: {myTypeObj2.IsLayoutSequential}."
with e ->
    printfn $"\nAn exception occurred: {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.ExplicitLayout和 TypeAttributes.SequentialLayout 列舉值表示該型態欄位在記憶體中的佈局方式。

對於動態型別,你可以指定 TypeAttributes.SequentialLayout 建立型別的時間。 在程式碼中,對型別套用 StructLayoutAttribute 帶有 LayoutKind.Sequential 枚舉值的屬性,以指定該版面為序列。

Note

你無法用這個 GetCustomAttributes 方法來判斷是否 StructLayoutAttribute 已經套用到某個類型。

欲了解更多資訊,請參閱 通用語言基礎架構(CLI)文件規範第 9.1.2 節「分割區 II:元資料定義與語意」。

若電流 Type 代表構造化的泛型,則此性質適用於該型別所由的泛型定義。 例如,若電流 Type 代表 MyGenericType<int> (MyGenericType(Of Integer) 在 Visual Basic 中),此屬性的值由 決定 MyGenericType<T>。

若電流 Type 代表一般型別或通用方法定義中的型別參數,則此性質總是回傳 false。

適用於

另請參閱