FieldInfo.GetValue(Object) Metoda

Definicja

Po zastąpieniu w klasie pochodnej zwraca wartość pola obsługiwanego przez dany obiekt.

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

Parametry

obj
Object

Obiekt, którego wartość pola zostanie zwrócona.

Zwraca

Obiekt zawierający wartość pola odzwierciedlonego przez to wystąpienie.

Implementuje

Wyjątki

Pole jest niestatyczne i obj ma wartość null.

Uwaga: w programie .NET dla aplikacji ze Sklepu Windows lub w bibliotece klas przenośnych przechwyć Exception zamiast tego.

Pole jest oznaczone literałem, ale pole nie ma jednego z akceptowanych typów literałów.

Obiekt wywołujący nie ma uprawnień dostępu do tego pola.

Uwaga: na platformie .NET dla aplikacji ze Sklepu Windows lub biblioteki klas przenośnych przechwyć wyjątek klasy bazowej , MemberAccessExceptionzamiast tego.

Metoda nie jest zadeklarowana ani dziedziczona przez klasę .obj

Przykłady

W poniższym przykładzie użyto GetValue metody w celu pobrania wartości pola statycznego. Zwróć uwagę, że wartość argumentu obj to 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

Poniższy przykład pobiera tablicę FieldInfo obiektów reprezentujących pola FieldsClass typu, a następnie wywołuje GetValue metodę , aby wyświetlić wartość każdego pola dla fieldsInst obiektu.

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'

Uwagi

Jeśli pole jest statyczne, obj jest ignorowane. W przypadku pól obj niestatyczne powinny być wystąpieniem klasy dziedziczonej lub deklarujące pole. Zwróć uwagę, że zwracany typ to GetValueObject. Jeśli na przykład pole zawiera wartość logiczną, zwracane jest wystąpienie Object z odpowiednią wartością logiczną. Przed zwróceniem wartości sprawdza, GetValue czy użytkownik ma uprawnienia dostępu.

Uwaga

Ograniczenia dostępu są ignorowane dla w pełni zaufanego kodu. Oznacza to, że dostęp do prywatnych konstruktorów, metod, pól i właściwości można uzyskiwać i wywoływać za pośrednictwem odbicia za każdym razem, gdy kod jest w pełni zaufany.

Uwaga

Począwszy od .NET Framework 2.0 z dodatkiem Service Pack 1, ta metoda może służyć do uzyskiwania dostępu do elementów członkowskich innych niż publiczne, jeśli obiekt wywołujący otrzymał ReflectionPermission flagęReflectionPermissionFlag.RestrictedMemberAccess, a zestaw dotacji niepublikowych członków jest ograniczony do zestawu dotacji osoby wywołującej lub jego podzbioru. (Zobacz Zagadnienia dotyczące zabezpieczeń dotyczące odbicia).

Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.

Dotyczy

Zobacz też