FieldAttributes Výčet

Definice

Určuje příznaky, které popisují atributy pole.

Tento výčet podporuje bitové kombinace hodnot jeho členů.

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
Dědičnost
FieldAttributes
Atributy

Pole

Name Hodnota Description
PrivateScope 0

Určuje, že pole nelze odkazovat.

Private 1

Určuje, že pole je přístupné pouze nadřazeným typem.

FamANDAssem 2

Určuje, že pole je přístupné pouze podtypy v tomto sestavení.

Assembly 3

Určuje, že pole je přístupné v celém sestavení.

Family 4

Určuje, že pole je přístupné pouze podle typu a podtypů.

FamORAssem 5

Určuje, že pole je přístupné podtypy kdekoli a také v celém tomto sestavení.

Public 6

Určuje, že pole je přístupné všem členům, pro které je tento obor viditelný.

FieldAccessMask 7

Určuje úroveň přístupu daného pole.

Static 16

Určuje, že pole představuje definovaný typ nebo jinak se jedná o instanci.

InitOnly 32

Určuje, že pole je inicializováno pouze a lze jej nastavit pouze v těle konstruktoru.

Literal 64

Určuje, že hodnota pole je konstanta kompilace (statická nebo časná vazba). Jakýkoli pokus o nastavení vyvolá chybu FieldAccessException.

NotSerialized 128

Určuje, že pole nemusí být serializováno při vzdáleném typu.

HasFieldRVA 256

Určuje, že pole má relativní virtuální adresu (RVA). RVA je umístění těla metody v aktuálním obrázku, jako adresa vzhledem k začátku souboru obrázku, ve kterém se nachází.

SpecialName 512

Určuje speciální metodu s názvem popisujícím, jak je metoda speciální.

RTSpecialName 1024

Určuje, že modul CLR (interní rozhraní API metadat) by měl zkontrolovat kódování názvů.

HasFieldMarshal 4096

Určuje, že pole obsahuje informace o zařazování.

PinvokeImpl 8192

Vyhrazeno pro budoucí použití.

HasDefault 32768

Určuje, že pole má výchozí hodnotu.

ReservedMask 38144

Rezervovaný.

Příklady

V tomto příkladu jsou vytvořena tři pole a FieldAttributes zobrazí se hodnoty. Hodnota FieldAttributes může obsahovat více než jeden atribut, například oba Public a Literal, jak je znázorněno ve třetím poli.

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

Poznámky

FieldAttributes používá hodnotu z FieldAccessMask k maskování pouze části hodnoty atributu, které se týkají přístupnosti. Následující kód například určuje, jestli Attributes má nastavenou veřejnou bitovou sadu.

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

Chcete-li získat FieldAttributes, nejprve získat třídu Type. Z Typezískejte FieldInfo. Z FieldInfozískejte Attributes.

Výčtová hodnota je číslo představující bitové OR atributů implementovaných v poli.

Platí pro