FieldAttributes 列舉

定義

指定描述欄位屬性的旗標。

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

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

欄位

名稱 Description
PrivateScope 0

指定欄位不可被參考。

Private 1

指定該欄位僅能由父類型存取。

FamANDAssem 2

指定該欄位僅能透過此組合中的子類型存取。

Assembly 3

規定該欄位在整個組裝過程中皆可存取。

Family 4

指定欄位僅可透過類型及子類型存取。

FamORAssem 5

指定該欄位可由子類型在任何地方及整個組裝過程中存取。

Public 6

規定該欄位可由任何可視此範圍的成員存取。

FieldAccessMask 7

指定特定欄位的存取層級。

Static 16

指定欄位代表定義型別,否則是每個實例。

InitOnly 32

指定欄位僅初始化,且只能設定在建構子的主體中。

Literal 64

指定欄位值為編譯時(靜態或早期束縛)常數。 任何嘗試設定它都會拋出一個 FieldAccessException

NotSerialized 128

規定當型別被遠端存取時,欄位不必序列化。

HasFieldRVA 256

指定欄位具有相對虛擬位址(RVA)。 RVA 是方法主體在當前影像中的位置,作為相對於其所在影像檔案起始位置的位址。

SpecialName 512

指定一種特殊方法,名稱描述該方法的特殊性。

RTSpecialName 1024

規定通用語言執行時(元資料內部 API)應檢查名稱編碼。

HasFieldMarshal 4096

指定欄位有編組資訊。

PinvokeImpl 8192

保留供未來使用。

HasDefault 32768

指定欄位有預設值。

ReservedMask 38144

已保留。

範例

在此範例中,建立三個欄位並顯示這些 FieldAttributes 值。 一個FieldAttributes值可以包含多個屬性,例如同時PublicLiteral和 ,如第三個欄位所示。

using System;
using System.Reflection;

public class Demo
{
    // Make three fields:
    // The first field is private.
    private string m_field = "String A";

    // The second field is public.
    public string Field = "String B";

    // The third field is public const (hence also literal and static),
    // with a default value.
    public const string FieldC = "String C";
}

public class Myfieldattributes
{
    public static void Main()
    {
        Console.WriteLine ("\nReflection.FieldAttributes");
        Demo d = new Demo();

        // Get a Type object for Demo, and a FieldInfo for each of
        // the three fields. Use the FieldInfo to display field
        // name, value for the Demo object in d, and attributes.
        //
        Type myType = typeof(Demo);
        FieldInfo fiPrivate = myType.GetField("m_field",
            BindingFlags.NonPublic | BindingFlags.Instance);
        DisplayField(d, fiPrivate);

        FieldInfo fiPublic = myType.GetField("Field",
            BindingFlags.Public | BindingFlags.Instance);
        DisplayField(d, fiPublic);

        FieldInfo fiConstant = myType.GetField("FieldC",
            BindingFlags.Public | BindingFlags.Static);
        DisplayField(d, fiConstant);
    }

    static void DisplayField(Object obj, FieldInfo f)
    {
        // Display the field name, value, and attributes.
        //
        Console.WriteLine("{0} = \"{1}\"; attributes: {2}",
            f.Name, f.GetValue(obj), f.Attributes);
    }
}

/* This code example produces the following output:

Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
 */
Imports System.Reflection

Public Class Demo
    ' Declare three fields.
    ' The first field is private.
    Private m_field As String = "String A"

    'The second field is public.
    Public Field As String = "String B"

    ' The third field is public and const, hence also static
    ' and literal with a default value.
    Public Const FieldC As String = "String C"

End Class

Module Module1
    Sub Main()
        ' Create an instance of the Demo class.
        Dim d As New Demo()

        Console.WriteLine(vbCrLf & "Reflection.FieldAttributes")

        ' Get a Type object for Demo, and a FieldInfo for each of
        ' the three fields. Use the FieldInfo to display field
        ' name, value for the Demo object in d, and attributes.
        '
        Dim myType As Type = GetType(Demo)

        Dim fiPrivate As FieldInfo = myType.GetField("m_field", _
            BindingFlags.NonPublic Or BindingFlags.Instance)
        DisplayField(d, fiPrivate)

        Dim fiPublic As FieldInfo = myType.GetField("Field", _
            BindingFlags.Public Or BindingFlags.Instance)
        DisplayField(d, fiPublic)

        Dim fiConstant As FieldInfo = myType.GetField("FieldC", _
            BindingFlags.Public Or BindingFlags.Static)
        DisplayField(d, fiConstant)
    End Sub

    Sub DisplayField(ByVal obj As Object, ByVal f As FieldInfo)

        ' Display the field name, value, and attributes.
        '
        Console.WriteLine("{0} = ""{1}""; attributes: {2}", _
            f.Name, f.GetValue(obj), f.Attributes)
    End Sub

End Module

' This code example produces the following output:
'
'm_field = "String A"; attributes: Private
'Field = "String B"; attributes: Public
'FieldC = "String C"; attributes: Public, Static, Literal, HasDefault

備註

FieldAttributes 使用 from FieldAccessMask 值來遮蔽屬性值中與可達性相關的部分。 例如,以下程式碼判斷公用位元是否 Attributes 設定。

FieldInfo fi = obj.GetType().GetField("field1");

if ((fi.Attributes & FieldAttributes.FieldAccessMask) ==
    FieldAttributes.Public)
{
    Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj));
}
Dim fi As FieldInfo = obj.GetType().GetField("field1")

If (fi.Attributes And FieldAttributes.FieldAccessMask) = _
    FieldAttributes.Public Then
    Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj))
End If

要取得 FieldAttributes,首先取得類別 Type。 從 Type中取得 FieldInfo。 從 FieldInfo中取得 Attributes

列舉值是一個數字,代表欄位上實作屬性的逐位元或值。

適用於