閱讀英文版本

分享方式:


FieldAttributes 列舉

定義

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

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

C#
[System.Flags]
public enum FieldAttributes
C#
[System.Flags]
[System.Serializable]
public enum FieldAttributes
C#
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FieldAttributes
繼承
FieldAttributes
屬性

欄位

名稱 Description
Assembly 3

指定在整個組件中欄位是可存取的。

FamANDAssem 2

指定欄位只能由這個組件中的子型別存取。

Family 4

指定欄位只能由型別和子型別存取。

FamORAssem 5

指定欄位可以在任何地方和整個組件中由子型別存取。

FieldAccessMask 7

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

HasDefault 32768

指定此欄位含有預設值。

HasFieldMarshal 4096

指定欄位含有封送處理 (Marshaling) 資訊。

HasFieldRVA 256

指定欄位含有相對虛擬位址 (RVA)。 RVA 為目前影像中方法主體的位置,就像相對於影像檔所在位置的開始位址。

InitOnly 32

指定欄位只初始化,而且只能在建構函式主體中設定。

Literal 64

指定欄位的值為編譯時期常數 (靜態或早期繫結), 任何對其進行設定的嘗試都會擲回 FieldAccessException

NotSerialized 128

指定當型別為遠端時,欄位不必序列化。

PinvokeImpl 8192

保留供未來使用。

Private 1

指定欄位只能由父型別存取。

PrivateScope 0

指定無法參考此欄位。

Public 6

指定欄位可以讓能見到這個範圍的任何成員存取。

ReservedMask 38144

保留的。

RTSpecialName 1024

指定 Common Language Runtime (中繼資料內部 API) 應檢查名稱編碼方式。

SpecialName 512

指定特殊方法,具有描述方法是如何特殊的名稱。

Static 16

指定表示定義型別的欄位,不然它就是一個執行個體。

範例

在此範例中,會建置三個字段,並 FieldAttributes 顯示值。 值 FieldAttributes 可以包含多個屬性,例如 PublicLiteral,如第三個字段所示。

C#
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
 */

備註

FieldAttributes 會使用 從 FieldAccessMask 遮罩的值,只遮罩與輔助功能相關的屬性值部分。 例如,下列程式代碼會判斷是否已 Attributes 設定公用位。

C#
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));
}

若要取得 FieldAttributes,請先取得 類別 Type。 從取得 TypeFieldInfo。 從取得 FieldInfoAttributes

列舉值是數位,代表欄位上實作之屬性的位 OR。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0