FieldInfo.GetValue(Object) Yöntem

Tanım

Türetilmiş bir sınıfta geçersiz kılındığında, belirli bir nesne tarafından desteklenen bir alanın değerini döndürür.

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

Parametreler

obj
Object

Alan değeri döndürülecek nesne.

Döndürülenler

Bu örnek tarafından yansıtılan alanın değerini içeren bir nesne.

Uygulamalar

Özel durumlar

Alan statik değildir ve obj şeklindedir null.

Not: Windows Mağazası uygulamaları için .NET'te veya Taşınabilir Sınıf Kitaplığı'nda bunun yerine yakalayın Exception .

Alan değişmez değer olarak işaretlenir, ancak alan kabul edilen değişmez değer türlerinden birine sahip değildir.

Çağıranın bu alana erişme izni yok.

Not: Windows Mağazası uygulamaları için .NET'te veya Taşınabilir Sınıf Kitaplığı'nda bunun yerine temel sınıf özel durumunu MemberAccessExceptionyakalayın.

yöntemi ne bildirilir ne de sınıfı tarafından devralınır obj.

Örnekler

Aşağıdaki örnek, statik bir alanın değerini almak için yöntemini kullanır GetValue . bağımsız değişkeninin değerinin obj olduğuna nulldikkat edin.

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

Aşağıdaki örnek, türün FieldInfo alanlarını FieldsClass temsil eden bir nesne dizisi alır ve ardından nesnesinin her alanının değerini görüntülemek için fieldsInst öğesini çağırırGetValue.

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'

Açıklamalar

Alan statikse yoksayılır obj . Statik olmayan alanlar için, obj alanı devralan veya bildiren bir sınıfın örneği olmalıdır. dönüş türünün GetValue olduğunu Objectunutmayın. Örneğin, alan bir Boole temel değeri barındırıyorsa, uygun Boole değerine sahip bir örneği Object döndürülür. Değeri döndürmeden önce kullanıcının GetValue erişim izni olup olmadığını denetler.

Not

Tam olarak güvenilen kod için erişim kısıtlamaları yoksayılır. Yani özel oluşturucular, yöntemler, alanlar ve özellikler, koda tam olarak güvenildiğinde yansıma üzerinden erişilebilir ve çağrılabilir.

Not

.NET Framework 2.0 Service Pack 1'den başlayarak bu yöntem, çağıranın bayrağıyla verilmiş ReflectionPermission olması ve ortak olmayan üyelerin izin kümesinin çağıranın izin kümesiyle veya bunun bir alt kümesiyle ReflectionPermissionFlag.RestrictedMemberAccess sınırlı olması durumunda genel olmayan üyelere erişmek için kullanılabilir. (Bkz. Yansıma için GüvenlikLe İlgili Dikkat Edilmesi Gerekenler.)

Bu işlevselliği kullanmak için uygulamanızın .NET Framework 3.5 veya sonraki bir sürümü hedeflemesi gerekir.

Şunlara uygulanır

Ayrıca bkz.