FieldAttributes Wyliczenie

Definicja

Określa flagi opisujące atrybuty pola.

To wyliczenie obsługuje bitową kombinację jego wartości składowych.

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
Dziedziczenie
FieldAttributes
Atrybuty

Pola

Assembly 3

Określa, że pole jest dostępne w całym zestawie.

FamANDAssem 2

Określa, że pole jest dostępne tylko przez podtypy w tym zestawie.

Family 4

Określa, że pole jest dostępne tylko według typu i podtypów.

FamORAssem 5

Określa, że pole jest dostępne dla podtypów w dowolnym miejscu, a także w całym tym zestawie.

FieldAccessMask 7

Określa poziom dostępu danego pola.

HasDefault 32768

Określa, że pole ma wartość domyślną.

HasFieldMarshal 4096

Określa, że pole ma informacje dotyczące marshalingu.

HasFieldRVA 256

Określa, że pole ma względny adres wirtualny (RVA). RVA to lokalizacja treści metody na bieżącym obrazie jako adres względem początku pliku obrazu, w którym się znajduje.

InitOnly 32

Określa, że pole jest inicjowane tylko i można ustawić tylko w treści konstruktora.

Literal 64

Określa, że wartość pola jest stałą czasu kompilacji (statyczną lub wczesną granicą). Każda próba ustawienia zgłasza błąd FieldAccessException.

NotSerialized 128

Określa, że pole nie musi być serializowane, gdy typ jest zdalny.

PinvokeImpl 8192

Zarezerwowane do użytku w przyszłości.

Private 1

Określa, że pole jest dostępne tylko przez typ nadrzędny.

PrivateScope 0

Określa, że nie można odwoływać się do pola.

Public 6

Określa, że pole jest dostępne dla każdego elementu członkowskiego, dla którego ten zakres jest widoczny.

ReservedMask 38144

Zarezerwowany.

RTSpecialName 1024

Określa, że środowisko uruchomieniowe języka wspólnego (wewnętrzne interfejsy API metadanych) powinno sprawdzać kodowanie nazw.

SpecialName 512

Określa specjalną metodę o nazwie opisującej sposób, w jaki metoda jest specjalna.

Static 16

Określa, że pole reprezentuje zdefiniowany typ, a w przeciwnym razie jest to wystąpienie.

Przykłady

W tym przykładzie są tworzone trzy pola, a FieldAttributes wartości są wyświetlane. Wartość FieldAttributes może zawierać więcej niż jeden atrybut, na przykład i Public Literal, jak pokazano w trzecim polu.

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

public ref class Demo
{
private:
    // Make three fields:
    // The first field is private.
    String^ m_field;

    // The second field is public.
public:
    String^ Field;

    // The third field is public and literal. 
    literal String^ FieldC = "String C";

    Demo() { m_field = "String A"; Field = "String B"; }
};

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

void main()
{
    Console::WriteLine ("\nReflection.FieldAttributes");
    Demo^ d = gcnew 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 = Demo::typeid;

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

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

Uwagi

FieldAttributes używa wartości z FieldAccessMask , aby zamaskować tylko części wartości atrybutu, które odnoszą się do ułatwień dostępu. Na przykład poniższy kod określa, czy Attributes ustawiono publiczny bit.

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

Aby uzyskać klasę FieldAttributes, najpierw pobierz klasę Type. W pliku Typepobierz element FieldInfo. W pliku FieldInfopobierz element Attributes.

Wyliczone wartości to liczba reprezentująca bitowe OR atrybutów zaimplementowanych w polu.

Dotyczy