FieldAttributes Énumération

Définition

Spécifie les indicateurs qui décrivent les attributs d’un champ.

Cette énumération prend en charge une combinaison au niveau du bit de ses valeurs membres.

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
Héritage
FieldAttributes
Attributs

Champs

Nom Valeur Description
PrivateScope 0

Spécifie que le champ ne peut pas être référencé.

Private 1

Spécifie que le champ est accessible uniquement par le type parent.

FamANDAssem 2

Spécifie que le champ est accessible uniquement par les sous-types de cet assembly.

Assembly 3

Spécifie que le champ est accessible dans l’assembly.

Family 4

Spécifie que le champ est accessible uniquement par type et sous-types.

FamORAssem 5

Spécifie que le champ est accessible par les sous-types n’importe où, ainsi que dans l’ensemble de cet assembly.

Public 6

Spécifie que le champ est accessible par n’importe quel membre pour lequel cette étendue est visible.

FieldAccessMask 7

Spécifie le niveau d’accès d’un champ donné.

Static 16

Spécifie que le champ représente le type défini ou qu’il est par instance.

InitOnly 32

Spécifie que le champ est initialisé uniquement et ne peut être défini que dans le corps d’un constructeur.

Literal 64

Spécifie que la valeur du champ est une constante de compilation (statique ou anticipée). Toute tentative de définition lève un FieldAccessException.

NotSerialized 128

Spécifie que le champ n’a pas besoin d’être sérialisé lorsque le type est distant.

HasFieldRVA 256

Spécifie que le champ a une adresse virtuelle relative (RVA). L’appliance virtuelle RVA est l’emplacement du corps de la méthode dans l’image actuelle, en tant qu’adresse par rapport au début du fichier image dans lequel il se trouve.

SpecialName 512

Spécifie une méthode spéciale, avec le nom décrivant comment la méthode est spéciale.

RTSpecialName 1024

Spécifie que le Common Language Runtime (API internes de métadonnées) doit vérifier l’encodage du nom.

HasFieldMarshal 4096

Spécifie que le champ contient des informations de marshaling.

PinvokeImpl 8192

Réservé pour une utilisation ultérieure.

HasDefault 32768

Spécifie que le champ a une valeur par défaut.

ReservedMask 38144

Réservé.

Exemples

Dans cet exemple, trois champs sont générés et les FieldAttributes valeurs sont affichées. Une FieldAttributes valeur peut contenir plusieurs attributs, par exemple, et PublicLiteral, comme indiqué dans le troisième champ.

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

Remarques

FieldAttributes utilise la valeur de FieldAccessMask pour masquer uniquement les parties de la valeur d’attribut qui se rapportent à l’accessibilité. Par exemple, le code suivant détermine si Attributes le bit public est défini.

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

Pour obtenir le FieldAttributes, commencez par obtenir la classe Type. À partir de la Type, obtenir le FieldInfo. À partir de la FieldInfo, obtenir le Attributes.

La valeur énumérée est un nombre représentant l’OR au niveau du bit des attributs implémentés sur le champ.

S’applique à