Type.IsAutoLayout 屬性

定義

取得表示目前類型的欄位是否已由 Common Language Runtime 自動配置版面的值。

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

屬性值

如果目前類型的 Attributes 屬性包含 AutoLayout則為 true,否則為 false

實作

範例

下列範例會建立 類型的實例,並顯示 IsAutoLayout 屬性。

#using <System.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;

// The MyDemoAttribute class is selected as AutoLayout.

[StructLayoutAttribute(LayoutKind::Auto)]
public ref class MyDemoAttribute{};

void MyAutoLayoutMethod( String^ typeName )
{
   try
   {
      
      // Create an instance of the Type class using the GetType method.
      Type^ myType = Type::GetType( typeName );
      
      // Get and display the IsAutoLayout property of the
      // MyDemoAttribute instance.
      Console::WriteLine( "\nThe AutoLayout property for the MyDemoAttribute is {0}.", myType->IsAutoLayout );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nAn exception occurred: {0}.", e->Message );
   }

}

int main()
{
   MyAutoLayoutMethod( "MyDemoAttribute" );
}
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);
    }
}
open System.Runtime.InteropServices

// The Demo class is attributed as AutoLayout.
[<StructLayoutAttribute(LayoutKind.Auto)>]
type Demo = class end

// Create an instance of the Type class using the GetType method.
let myType = typeof<Demo>
// Get and display the IsAutoLayout property of the
// Demoinstance.
printfn $"\nThe AutoLayout property for the Demo class is {myType.IsAutoLayout}."
Imports System.Runtime.InteropServices

' The Demo class is has the AutoLayout attribute.
<StructLayoutAttribute(LayoutKind.Auto)> _
Public Class Demo
End Class 

Public Class Example
    Public Shared Sub Main()
        ' Get the Type object for the Demo class.
        Dim myType As Type = GetType(Demo)
        ' Get and display the IsAutoLayout property of the 
        ' Demo class.
        Console.WriteLine("The AutoLayout property for the Demo class is '{0}'.", _
            myType.IsAutoLayout.ToString())
    End Sub 
End Class

備註

這個屬性會以方便的方式提供。 或者,您可以使用 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 回 。

適用於

另請參閱