Type.IsLayoutSequential 屬性

定義

取得表示目前類型的欄位是否已依為其定義或發出至中繼資料之順序,循序配置版面的值。Gets a value indicating whether the fields of the current type are laid out sequentially, in the order that they were defined or emitted to the metadata.

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

屬性值

如果目前類型的 true 屬性包含 Attributes 則為 SequentialLayout,否則為 falsetrue if the Attributes property of the current type includes SequentialLayout; otherwise, false.

實作

範例

下列範例會建立類別的實例,其中已設定 StructLayoutAttribute 類別中的 LayoutKind.Sequential 列舉值、檢查 IsLayoutSequential 屬性,並顯示結果。The following example creates an instance of a class for which the LayoutKind.Sequential enumeration value in the StructLayoutAttribute class has been set, checks for the IsLayoutSequential property, and displays the result.

#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

備註

提供這個屬性是為了方便起見。This property is provided as a convenience. 或者,您可以使用 TypeAttributes.LayoutMask 列舉值來選取類型配置屬性,然後測試是否已設定 TypeAttributes.SequentialLayoutAlternatively, you can use the TypeAttributes.LayoutMask enumeration value to select the type layout attributes, and then test whether TypeAttributes.SequentialLayout is set. TypeAttributes.AutoLayoutTypeAttributes.ExplicitLayoutTypeAttributes.SequentialLayout 列舉值會指出類型欄位在記憶體中配置的方式。The TypeAttributes.AutoLayout, TypeAttributes.ExplicitLayout, and TypeAttributes.SequentialLayout enumeration values indicate the way the fields of the type are laid out in memory.

若為動態類型,您可以在建立類型時指定 TypeAttributes.SequentialLayoutFor dynamic types, you can specify TypeAttributes.SequentialLayout when you create the type. 在程式碼中,將具有 LayoutKind.Sequential 列舉值的 StructLayoutAttribute 屬性套用至類型,以指定該版面配置為順序。In code, apply the StructLayoutAttribute attribute with the LayoutKind.Sequential enumeration value to the type, to specify that layout is sequential.

注意

您無法使用 GetCustomAttributes 方法來判斷 StructLayoutAttribute 是否已套用至類型。You cannot use the GetCustomAttributes method to determine whether the StructLayoutAttribute has been applied to a type.

如需詳細資訊,請參閱通用語言基礎結構(CLI)檔之規格的 < 區段9.1.2 一節(「分割區 II:元資料定義和語法」)。For more information, see section 9.1.2 of the specification for the Common Language Infrastructure (CLI) documentation, "Partition II: Metadata Definition and Semantics". 您可以線上取得這份文件;請參閱 MSDN 上的 ECMA C# 和通用語言基礎結構標準,以及 Ecma International 網站上的標準 ECMA-335 - 通用語言基礎結構 (CLI)The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

如果目前的 Type 代表結構化的泛型型別,這個屬性會套用至用來建立類型的泛型型別定義。If the current Type represents a constructed generic type, this property applies to the generic type definition from which the type was constructed. 例如,如果目前的 Type 表示 Visual Basic 中的 MyGenericType<int>MyGenericType(Of Integer)),則這個屬性的值是由 MyGenericType<T>所決定。For example, if the current Type represents MyGenericType<int> (MyGenericType(Of Integer) in Visual Basic), the value of this property is determined by MyGenericType<T>.

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,則這個屬性一律會傳回 falseIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

適用於

另請參閱