FieldAttributes Enumerazione

Definizione

Specifica i flag che descrivono gli attributi di un campo.

Questa enumerazione supporta una combinazione bit per bit dei rispettivi valori dei membri.

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
Ereditarietà
FieldAttributes
Attributi

Campi

Nome Valore Descrizione
PrivateScope 0

Specifica che non è possibile fare riferimento al campo.

Private 1

Specifica che il campo è accessibile solo dal tipo padre.

FamANDAssem 2

Specifica che il campo è accessibile solo dai sottotipi in questo assembly.

Assembly 3

Specifica che il campo è accessibile in tutto l'assembly.

Family 4

Specifica che il campo è accessibile solo per tipo e sottotipi.

FamORAssem 5

Specifica che il campo è accessibile da sottotipi ovunque e in tutto l'assembly.

Public 6

Specifica che il campo è accessibile da qualsiasi membro per il quale questo ambito è visibile.

FieldAccessMask 7

Specifica il livello di accesso di un determinato campo.

Static 16

Specifica che il campo rappresenta il tipo definito oppure che è per istanza.

InitOnly 32

Specifica che il campo viene inizializzato solo e può essere impostato solo nel corpo di un costruttore.

Literal 64

Specifica che il valore del campo è una costante in fase di compilazione (statica o anticipata). Qualsiasi tentativo di impostarlo genera un'eccezione FieldAccessException.

NotSerialized 128

Specifica che il campo non deve essere serializzato quando il tipo è remoto.

HasFieldRVA 256

Specifica che il campo ha un indirizzo virtuale relativo. L'RVA è la posizione del corpo del metodo nell'immagine corrente, come indirizzo relativo all'inizio del file di immagine in cui si trova.

SpecialName 512

Specifica un metodo speciale, con il nome che descrive come il metodo è speciale.

RTSpecialName 1024

Specifica che Common Language Runtime (API interne dei metadati) deve controllare la codifica dei nomi.

HasFieldMarshal 4096

Specifica che il campo dispone di informazioni di marshalling.

PinvokeImpl 8192

Riservato a un uso futuro.

HasDefault 32768

Specifica che il campo ha un valore predefinito.

ReservedMask 38144

Riservato.

Esempio

In questo esempio vengono compilati tre campi e vengono visualizzati i FieldAttributes valori. Un FieldAttributes valore può contenere più attributi, ad esempio, e PublicLiteral, come illustrato nel terzo campo.

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

Commenti

FieldAttributes usa il valore da FieldAccessMask per mascherare solo le parti del valore dell'attributo che riguardano l'accessibilità. Ad esempio, il codice seguente determina se Attributes ha il bit pubblico impostato.

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

Per ottenere , ottenere FieldAttributesprima la classe Type. TypeDa ottenere .FieldInfo FieldInfoDa ottenere .Attributes

Il valore enumerato è un numero che rappresenta l'OR bit per bit degli attributi implementati nel campo.

Si applica a