FieldAttributes Enumeración

Definición

Especifica las marcas que describen los atributos de un campo.

Esta enumeración admite una combinación bit a bit de sus valores de miembro.

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
Herencia
FieldAttributes
Atributos

Campos

Assembly 3

Especifica que se puede obtener acceso al campo en todo el ensamblado.

FamANDAssem 2

Especifica que sólo los subtipos de este ensamblado pueden obtener acceso al campo en cuestión.

Family 4

Especifica que sólo se puede obtener acceso al campo mediante tipos y subtipos.

FamORAssem 5

Especifica que se puede obtener acceso al campo mediante subtipos en cualquier parte, así como en todo el ensamblado.

FieldAccessMask 7

Especifica el nivel de acceso de un campo dado.

HasDefault 32768

Especifica que el campo tiene un valor predeterminado.

HasFieldMarshal 4096

Especifica que el campo contiene información de cálculo de referencias.

HasFieldRVA 256

Especifica que el campo tiene una dirección relativa virtual (RVA). La RVA es la ubicación del cuerpo del método que se encuentra en la imagen principal, como por ejemplo, una dirección relativa al principio del archivo de imagen donde se encuentra.

InitOnly 32

Especifica que solo se inicializa el campo y solo se puede establecer en el cuerpo de un constructor.

Literal 64

Especifica que el valor del campo es una constante (estática o de enlace en tiempo de diseño) en tiempo de compilación. Cualquier intento de establecerlo produce una FieldAccessException.

NotSerialized 128

Especifica que no es necesario serializar el campo cuando el tipo tiene acceso remoto.

PinvokeImpl 8192

Reservado para un uso futuro.

Private 1

Especifica que sólo se puede obtener acceso al campo mediante los tipos principales.

PrivateScope 0

Indica que no se pueden crear referencias al campo.

Public 6

Especifica que cualquier miembro, para el cual este ámbito sea visible, puede obtener acceso al campo.

ReservedMask 38144

Reservado.

RTSpecialName 1024

Especifica que Common Language Runtime (API de metadatos internas) debe comprobar la codificación de nombres.

SpecialName 512

Especifica un método especial y su nombre describe en qué sentido es especial dicho método.

Static 16

Especifica que el campo representa el tipo definido, o bien es un ejemplo.

Ejemplos

En este ejemplo, se compilan tres campos y se muestran los FieldAttributes valores. Un FieldAttributes valor puede contener más de un atributo, por ejemplo, y Public Literal, como se muestra en el tercer campo.

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

Comentarios

FieldAttributes usa el valor de FieldAccessMask para enmascarar solo las partes del valor del atributo que pertenecen a la accesibilidad. Por ejemplo, el código siguiente determina si Attributes tiene establecido el bit público.

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

Para obtener , FieldAttributesobtenga primero la clase Type. TypeEn , obtenga .FieldInfo FieldInfoEn , obtenga .Attributes

El valor enumerado es un número que representa el or bit a bit de los atributos implementados en el campo.

Se aplica a