FieldInfo.GetValue(Object) Metoda

Definice

Při přepsání v odvozené třídě vrátí hodnotu pole podporovaného daným objektem.

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

Objekt, jehož hodnota pole bude vrácena.

Návraty

Objekt obsahující hodnotu pole reflektované touto instancí.

Implementuje

Výjimky

Pole není statické a obj je null.

Poznámka: V .NET pro aplikace pro Windows Store nebo v přenosné knihovně tříd místo toho zachyťte Exception .

Pole je označeno jako literál, ale pole nemá jeden z přijatých typů literálů.

Volající nemá oprávnění pro přístup k tomuto poli.

Poznámka: V .NET pro aplikace pro Windows Store nebo v přenosné knihovně tříd místo toho zachyťte výjimku MemberAccessExceptionzákladní třídy.

Metoda není deklarována ani neděděna třídou .obj

Příklady

Následující příklad používá metodu GetValue k načtení hodnoty statického pole. Všimněte si, že hodnota argumentu obj je 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

Následující příklad načte matici FieldInfo objektů, která představuje pole FieldsClass typu, a pak voláním GetValue metody zobrazí hodnotu každého pole pro fieldsInst objekt.

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'

Poznámky

Pokud je pole statické, obj bude ignorováno. Pro nestatické pole by měla být instance třídy, obj která zdědí nebo deklaruje pole. Všimněte si, že návratový GetValue typ je Object. Pokud například pole obsahuje logickou primitivní hodnotu, vrátí se instance Object s příslušnou logickou hodnotou. Před vrácením hodnoty zkontroluje, GetValue jestli má uživatel oprávnění k přístupu.

Poznámka

Omezení přístupu se u plně důvěryhodného kódu ignorují. To znamená, že k soukromým konstruktorům, metodám, polím a vlastnostem lze přistupovat a vyvolat je prostřednictvím reflexe vždy, když je kód plně důvěryhodný.

Poznámka

Počínaje aktualizací .NET Framework 2.0 Service Pack 1 lze tuto metodu použít pro přístup k neveřejným členůmReflectionPermissionFlag.RestrictedMemberAccess, pokud byl volajícímu udělen ReflectionPermission příznak a pokud je sada udělení neveřejných členů omezena na sadu udělení volajícího nebo její podmnožinu. (Viz Aspekty zabezpečení pro reflexi.)

Chcete-li použít tuto funkci, vaše aplikace by měla cílit na rozhraní .NET Framework 3.5 nebo novější.

Platí pro

Viz také