Type.IsLayoutSequential Egenskap

Definition

Hämtar ett värde som anger om fälten av den aktuella typen anges sekventiellt, i den ordning som de har definierats eller genererats till metadata.

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

Egenskapsvärde

true om egenskapen Attributes för den aktuella typen innehåller SequentialLayout, annars , false.

Implementeringar

Exempel

I följande exempel skapas en instans av en klass för vilken LayoutKind.Sequential uppräkningsvärdet i StructLayoutAttribute klassen har angetts, söker IsLayoutSequential efter egenskapen och visar resultatet.

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

Kommentarer

Den här egenskapen tillhandahålls som en bekvämlighet. Du kan också använda TypeAttributes.LayoutMask uppräkningsvärdet för att välja typlayoutattributen och sedan testa om TypeAttributes.SequentialLayout har angetts. Uppräkningsvärdena TypeAttributes.AutoLayout, TypeAttributes.ExplicitLayoutoch TypeAttributes.SequentialLayout anger hur fälten för typen anges i minnet.

För dynamiska typer kan du ange TypeAttributes.SequentialLayout när du skapar typen. I kod använder du StructLayoutAttribute attributet med LayoutKind.Sequential uppräkningsvärdet för typen för att ange att layouten är sekventiell.

Note

Du kan inte använda GetCustomAttributes metoden för att avgöra om har StructLayoutAttribute tillämpats på en typ.

Mer information finns i avsnitt 9.1.2 i specifikationen för CLI-dokumentationen (Common Language Infrastructure), "Partition II: Metadata Definition and Semantics".

Om strömmen Type representerar en konstruerad allmän typ gäller den här egenskapen för den generiska typdefinition som typen skapades från. Om den aktuella Type till exempel representerar MyGenericType<int> (MyGenericType(Of Integer) i Visual Basic) bestäms värdet för den här egenskapen av MyGenericType<T>.

Om strömmen Type representerar en typparameter i definitionen av en allmän typ eller allmän metod returnerar falseden här egenskapen alltid .

Gäller för

Se även