共用方式為


TypeAttributes 列舉

定義

指定類型屬性。

此列舉支援其成員值的位元組合。

public enum class TypeAttributes
[System.Flags]
public enum TypeAttributes
[System.Flags]
[System.Serializable]
public enum TypeAttributes
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum TypeAttributes
[<System.Flags>]
type TypeAttributes = 
[<System.Flags>]
[<System.Serializable>]
type TypeAttributes = 
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TypeAttributes = 
Public Enum TypeAttributes
繼承
TypeAttributes
屬性

欄位

名稱 Description
AnsiClass 0

LPTSTR 被解釋為 ANSI。

AutoLayout 0

規定類別欄位會自動由通用語言執行時規劃。

Class 0

指定型別為類別。

NotPublic 0

明確規定該類別非公開。

Public 1

指定該類別為公開。

NestedPublic 2

指定該類別巢狀且公開可見。

NestedPrivate 3

指定類別巢狀且帶有私有可見性。

NestedFamily 4

指定該類別以族可視性巢狀,因此只能透過其自身型別及任何派生型別中的方法存取。

NestedAssembly 5

指定該類別以組合語言可見性巢狀,因此只能透過其組合語言內的方法存取。

NestedFamANDAssem 6

指定該類別與組合語言族的可見性為巢狀,因此只能透過位於其族與組合語言交集的方法來存取。

NestedFamORAssem 7

指定該類別與族或組裝檔的可視性巢狀,因此只能透過位於族與組裝聯結中的方法存取。

VisibilityMask 7

指定型別可見性資訊。

SequentialLayout 8

規定類別欄位依欄位發出至元資料的順序依序排列。

ExplicitLayout 16

指定類別欄位在指定的偏移量處排列。

ExtendedLayout 24
LayoutMask 24

指定類別配置資訊。

ClassSemanticsMask 32

指定類別語意資訊;目前的類別是情境型(else agile)。

Interface 32

指定型別為介面。

Abstract 128

指定該類型為抽象型。

Sealed 256

指定該類別為具體且無法擴展。

SpecialName 1024

以名稱表示該類別的特殊性。

RTSpecialName 2048

執行時應該會檢查名稱編碼。

Import 4096

指定類別或介面是從其他模組匯入的。

Serializable 8192

規定類別可以被序列化。

WindowsRuntime 16384

指定 Windows 執行時型別。

UnicodeClass 65536

LPTSTR 被解讀為 UNICODE。

AutoClass 131072

LPTSTR 會自動解讀。

CustomFormatClass 196608

LPSTR 會被某些實作特定的方式解釋,包括拋出 NotSupportedException。 未用於 Microsoft 的 .NET Framework 實作。

StringFormatMask 196608

用於擷取字串資訊以促進原生互通性。

HasSecurity 262144

類型帶有安全性。

ReservedMask 264192

屬性保留給執行時使用。

BeforeFieldInit 1048576

規定呼叫該型態的靜態方法不會強制系統初始化該型別。

CustomFormatMask 12582912

用於擷取非標準編碼資訊,以實現原生互操作。 這兩個位元的數值意義尚未明確說明。 未用於 Microsoft 的 .NET Framework 實作。

範例

以下範例會取得代表多種不同類型物件的 Attributes 屬性 Type 值,並判斷是否設定了個別屬性旗標。

using System;
using System.Reflection;

internal struct S
{
    public int X;
}

public abstract class Example
{
    protected sealed class NestedClass {}

    public interface INested {}

    public static void Main()
    {
        // Create an array of types.
        Type[] types = { typeof(Example), typeof(NestedClass),
                         typeof(INested), typeof(S) };

        foreach (var t in types) 
        {
           Console.WriteLine("Attributes for type {0}:", t.Name);

           TypeAttributes attr = t.Attributes;

           // To test for visibility attributes, you must use the visibility mask.
           TypeAttributes visibility = attr & TypeAttributes.VisibilityMask;
           switch (visibility)
           {
               case TypeAttributes.NotPublic:
                   Console.WriteLine("   ...is not public");
                   break;
               case TypeAttributes.Public:
                   Console.WriteLine("   ...is public");
                   break;
               case TypeAttributes.NestedPublic:
                   Console.WriteLine("   ...is nested and public");
                   break;
               case TypeAttributes.NestedPrivate:
                   Console.WriteLine("   ...is nested and private");
                   break;
               case TypeAttributes.NestedFamANDAssem:
                   Console.WriteLine("   ...is nested, and inheritable only within the assembly" +
                      "\n         (cannot be declared in C#)");
                   break;
               case TypeAttributes.NestedAssembly:
                   Console.WriteLine("   ...is nested and internal");
                   break;
               case TypeAttributes.NestedFamily:
                   Console.WriteLine("   ...is nested and protected");
                   break;
               case TypeAttributes.NestedFamORAssem:
                   Console.WriteLine("   ...is nested and protected internal");
                   break;
           }

           // Use the layout mask to test for layout attributes.
           TypeAttributes layout = attr & TypeAttributes.LayoutMask;
           switch (layout)
           {
               case TypeAttributes.AutoLayout:
                   Console.WriteLine("   ...is AutoLayout");
                   break;
               case TypeAttributes.SequentialLayout:
                   Console.WriteLine("   ...is SequentialLayout");
                   break;
               case TypeAttributes.ExplicitLayout:
                   Console.WriteLine("   ...is ExplicitLayout");
                   break;
           }

           // Use the class semantics mask to test for class semantics attributes.
           TypeAttributes classSemantics = attr & TypeAttributes.ClassSemanticsMask;
           switch (classSemantics)
           {
               case TypeAttributes.Class:
                   if (t.IsValueType)
                   {
                       Console.WriteLine("   ...is a value type");
                   }
                   else
                   {
                       Console.WriteLine("   ...is a class");
                   }
                   break;
               case TypeAttributes.Interface:
                   Console.WriteLine("   ...is an interface");
                   break;
           }

           if ((attr & TypeAttributes.Abstract) != 0)
           {
               Console.WriteLine("   ...is abstract");
           }

           if ((attr & TypeAttributes.Sealed) != 0)
           {
               Console.WriteLine("   ...is sealed");
           }
           
           Console.WriteLine();
       }
    }
}
// The example displays the following output:
// Attributes for type Example:
//    ...is public
//    ...is AutoLayout
//    ...is a class
//    ...is abstract

// Attributes for type NestedClass:
//    ...is nested and protected
//    ...is AutoLayout
//    ...is a class
//    ...is sealed

// Attributes for type INested:
//    ...is nested and public
//    ...is AutoLayout
//    ...is an interface
//    ...is abstract

// Attributes for type S:
//    ...is not public
//    ...is SequentialLayout
//    ...is a value type
//    ...is sealed
Imports System.Reflection

Friend Structure S
    Public X As Integer
End Structure

Public MustInherit Class Example
    Protected NotInheritable Class NestedClass
    End Class

    Public Interface INested
    End Interface

    Public Shared Sub Main()
        ' Create an array of types.
        Dim types() As Type = { GetType(Example), GetType(NestedClass),
                                GetType(INested), GetType(S) }

        For Each t In types
           Console.WriteLine("Attributes for type {0}:", t.Name)

           Dim attr As TypeAttributes = t.Attributes

           ' Use the visibility mask to test for visibility attributes.
           Dim visibility As TypeAttributes = attr And TypeAttributes.VisibilityMask
           Select Case visibility
               Case TypeAttributes.NotPublic:
                   Console.WriteLine("   ...is not Public")
               Case TypeAttributes.Public:
                   Console.WriteLine("   ...is Public")
               Case TypeAttributes.NestedPublic:
                   Console.WriteLine("   ...is nested and Public")
               Case TypeAttributes.NestedPrivate:
                   Console.WriteLine("   ...is nested and Private")
               Case TypeAttributes.NestedFamANDAssem:
                   Console.WriteLine("   ...is nested, and inheritable only within the assembly" & _
                      vbLf & "         (cannot be declared in Visual Basic)")
               Case TypeAttributes.NestedAssembly:
                   Console.WriteLine("   ...is nested and Friend")
               Case TypeAttributes.NestedFamily:
                   Console.WriteLine("   ...is nested and Protected")
               Case TypeAttributes.NestedFamORAssem:
                   Console.WriteLine("   ...is nested and Protected Friend")
           End Select

           ' Use the layout mask to test for layout attributes.
           Dim layout As TypeAttributes = attr And TypeAttributes.LayoutMask
           Select Case layout
               Case TypeAttributes.AutoLayout:
                   Console.WriteLine("   ...is AutoLayout")
               Case TypeAttributes.SequentialLayout:
                   Console.WriteLine("   ...is SequentialLayout")
               Case TypeAttributes.ExplicitLayout:
                   Console.WriteLine("   ...is ExplicitLayout")
           End Select

           ' Use the class semantics mask to test for class semantics attributes.
           Dim classSemantics As TypeAttributes = attr And TypeAttributes.ClassSemanticsMask
           Select Case classSemantics
               Case TypeAttributes.Class:
                   If t.IsValueType Then
                       Console.WriteLine("   ...is a value type")
                   Else
                       Console.WriteLine("   ...is a class")
                   End If
               Case TypeAttributes.Interface:
                   Console.WriteLine("   ...is an interface")
           End Select

           If 0 <> (attr And TypeAttributes.Abstract) Then _
               Console.WriteLine("   ...is MustInherit")

           If 0 <> (attr And TypeAttributes.Sealed) Then _
               Console.WriteLine("   ...is NotInheritable")
           Console.WriteLine()
       Next
    End Sub
End Class
' The example displays the following output:
'       Attributes for type Example:
'          ...is Public
'          ...is AutoLayout
'          ...is a class
'          ...is MustInherit
'
'       Attributes for type NestedClass:
'          ...is nested and Protected
'          ...is AutoLayout
'          ...is a class
'          ...is NotInheritable
'
'       Attributes for type INested:
'          ...is nested and Public
'          ...is AutoLayout
'          ...is an interface
'          ...is MustInherit
'
'       Attributes for type S:
'          ...is not Public
'          ...is SequentialLayout
'          ...is a value type
'          ...is NotInheritable

備註

列舉中的部分成員 TypeAttributes 是遮罩,代表一組互斥屬性。 例如,成員VisibilityMask包含 NotPublicPublicNestedFamilyNestedPrivateNestedAssemblyNestedPublicNestedFamANDAssem和 。NestedFamORAssem 由於每個屬性集合都包含一個底層值為零的成員,你應該先 And 取得遮罩值,並取得特定 System.Reflection.TypeAttributes 值,例如屬性 Type.Attributes。 下表列出面罩及其成員:

Mask Includes
可見度遮罩 非公開
公共
巢狀公共
巢狀私人
巢狀家族
巢狀組裝
巢狀家族與母語
巢狀家族(NestedFamORAssem)
LayoutMask 佈局遮罩 自動佈局
順序佈局
ExplicitLayout
ClassSemanticsMask
介面
StringFormatMask 字串格式遮罩 AnsiClass
UnicodeClass
自動組
CustomFormatClass
CustomFormatMask 沒有成員。

此列舉器類別的成員與 cortypeAttr 列舉器相符,該列舉器定義在 corhdr.h 檔案中。

適用於