FieldInfo.GetValue(Object) Metodo

Definizione

In caso di override in una classe derivata, restituisce il valore di un campo supportato da un determinato oggetto.

public:
 abstract System::Object ^ GetValue(System::Object ^ obj);
public abstract object GetValue (object obj);
public abstract object? GetValue (object? obj);
abstract member GetValue : obj -> obj
Public MustOverride Function GetValue (obj As Object) As Object

Parametri

obj
Object

Oggetto di cui verrà restituito il valore del campo.

Restituisce

Oggetto contenente il valore del campo riflesso da questa istanza.

Implementazioni

Eccezioni

Il campo non è statico e obj è null.

Nota: in .NET per le app di Windows Store o la libreria di classi portabile, rilevareException invece.

Un campo viene contrassegnato come valore letterale, ma non contiene uno dei tipi letterali accettati.

Il chiamante non ha l'autorizzazione necessaria per accedere a questo campo.

Nota: in .NET per le app di Windows Store o la libreria di classi portabili, rilevare invece l'eccezione della classe di base, , MemberAccessException.

Il metodo non viene dichiarato né ereditato dalla classe di obj.

Esempio

Nell'esempio seguente viene usato il metodo per recuperare il GetValue valore di un campo statico. Si noti che il valore dell'argomento obj è null.

using namespace System;
using namespace System::Reflection;

ref class Example
{
  public:
     static String^ val = "test";
};

int main()
{
   FieldInfo^ fld = Example::typeid->GetField( "val" );
   Console::WriteLine(fld->GetValue(nullptr) );
   Example::val = "hi";
   Console::WriteLine(fld->GetValue(nullptr) );
}
// The example displays the following output:
//     test
//     hi
using System;
using System.Reflection;

class Example
{
    public static String val = "test";

    public static void Main()
    {
        FieldInfo fld = typeof(Example).GetField("val");
        Console.WriteLine(fld.GetValue(null));
        val = "hi";
        Console.WriteLine(fld.GetValue(null));
    }
}
// The example displays the following output:
//     test
//     hi
Imports System.Reflection

Class Example
    Public Shared val As String = "test"
    
    Public Shared Sub Main()
        Dim fld As FieldInfo = GetType(Example).GetField("val")
        Console.WriteLine(fld.GetValue(Nothing))
        val = "hi"
        Console.WriteLine(fld.GetValue(Nothing))
    End Sub 
End Class 
' The example displays the following output:
'     test
'     hi

Nell'esempio seguente viene recuperata una matrice di FieldInfo oggetti che rappresenta i campi del tipo e quindi chiama l'oggetto GetValue per visualizzare il valore di FieldsClass ogni campo per l'oggettofieldsInst.

using namespace System;
using namespace System::Reflection;

public ref class FieldsClass
{
  public:
     String^ fieldA;
     String^ fieldB;

     FieldsClass()
     {
        fieldA = "A public field";
        fieldB = "Another public field";
     }
};

int main()
{
   FieldsClass^ fieldsInst = gcnew FieldsClass;
   
   // Get the type of FieldsClass.
   Type^ fieldsType = FieldsClass::typeid;

   // Get the FieldInfo of FieldsClass.
   array<FieldInfo^>^ fields = fieldsType->GetFields(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));

   // Display the values of the fields.
   Console::WriteLine("Displaying the values of the fields of {0}:", fieldsType);
   for (int i = 0; i < fields->Length; i++)
   {
      Console::WriteLine("   {0}:\t'{1}'", 
                         fields[i]->Name, fields[i]->GetValue(fieldsInst));
   }
}
// The example displays the following output:
//     Displaying the values of the fields of FieldsClass:
//        fieldA:      'A public field'
//        fieldB:      'Another public field'
using System;
using System.Reflection;

public class FieldsClass
{
    public string fieldA;
    public string fieldB;

    public FieldsClass()
    {
        fieldA = "A public field";
        fieldB = "Another public field";
    }
}

public class Example
{
    public static void Main()
    {
        FieldsClass fieldsInst = new FieldsClass();
        // Get the type of FieldsClass.
        Type fieldsType = typeof(FieldsClass);

        // Get an array of FieldInfo objects.
        FieldInfo[] fields = fieldsType.GetFields(BindingFlags.Public
            | BindingFlags.Instance);
        // Display the values of the fields.
        Console.WriteLine("Displaying the values of the fields of {0}:",
            fieldsType);
        for(int i = 0; i < fields.Length; i++)
        {
            Console.WriteLine("   {0}:\t'{1}'",
                fields[i].Name, fields[i].GetValue(fieldsInst));
        }
    }
}
// The example displays the following output:
//     Displaying the values of the fields of FieldsClass:
//        fieldA:      'A public field'
//        fieldB:      'Another public field'
Imports System.Reflection

Public Class FieldsClass
    Public fieldA As String
    Public fieldB As String

    Public Sub New()
        fieldA = "A public field"
        fieldB = "Another public field"
    End Sub 
End Class 

Public Module Example
    Public Sub Main()
        Dim fieldsInst As New FieldsClass()
        ' Get the type of FieldsClass.
        Dim fieldsType As Type = GetType(FieldsClass)

        ' Get an array of FieldInfo objects.
        Dim fields As FieldInfo() = fieldsType.GetFields(BindingFlags.Public Or BindingFlags.Instance)
        ' Display the values of the fields.
        Console.WriteLine("Displaying the values of the fields of {0}:", fieldsType)
        For i As Integer = 0 To fields.Length - 1
            Console.WriteLine("   {0}:{2}'{1}'",
                fields(i).Name, fields(i).GetValue(fieldsInst), vbTab)
        Next 
    End Sub 
End Module
' The example displays the following output:
'     Displaying the values of the fields of FieldsClass:
'        fieldA:      'A public field'
'        fieldB:      'Another public field'

Commenti

Se il campo è statico, obj viene ignorato. Per i campi non statici, obj deve essere un'istanza di una classe che eredita o dichiara il campo. Si noti che il tipo restituito di GetValue è Object. Ad esempio, se il campo contiene un valore primitivo booleano, viene restituita un'istanza di Object con il valore booleano appropriato. Prima di restituire il valore, GetValue verifica se l'utente ha l'autorizzazione di accesso.

Nota

Le restrizioni di accesso vengono ignorate per il codice completamente attendibile. Ovvero, i costruttori privati, i metodi, i campi e le proprietà possono essere accessibili e richiamati tramite reflection ogni volta che il codice è completamente attendibile.

Nota

A partire da .NET Framework 2.0 Service Pack 1, questo metodo può essere usato per accedere a membri non pubblici se il chiamante è stato concesso ReflectionPermission con il ReflectionPermissionFlag.RestrictedMemberAccess flag e se il set di concessione dei membri non pubblici è limitato al set di concessione del chiamante o a un subset. Vedere Considerazioni sulla sicurezza per Reflection.

Per usare questa funzionalità, l'applicazione deve essere destinato a .NET Framework 3.5 o versione successiva.

Si applica a

Vedi anche