共用方式為


FieldInfo.GetValue(Object) 方法

定義

在衍生類別中覆寫時,傳回指定物件所支援欄位的值。

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

參數

obj
Object

將傳回其欄位值的物件。

傳回

物件,包含這個執行個體所反映欄位的值。

實作

例外狀況

欄位為非靜態,而 objnull

注意:在 適用於 Windows 市集應用程式的 .NET可攜式類別庫中,改為攔截 Exception

欄位標記為常值,但欄位沒有其中一個可接受的常值類型。

呼叫端沒有存取這個欄位的權限。

注意:在 適用於 Windows 市集應用程式的 .NET可攜式類別庫中,改為攔截基類例外狀況 MemberAccessException

obj 的類別未宣告也未繼承這個方法。

範例

下列範例會 GetValue 使用 方法來擷取靜態字段的值。 請注意,自變數的值 objnull

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

下列範例會擷取代表型別欄位的物件FieldsClass陣列FieldInfo,然後呼叫 GetValue 來顯示物件每個欄位fieldsInst的值。

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'

備註

如果欄位是靜態的, obj 則會忽略。 對於非靜態欄位, obj 應該是繼承或宣告欄位之類別的實例。 請注意, 傳 GetValue 回型別為 Object。 例如,如果欄位保留布爾基本值,則會傳回具有適當布爾值的 實例 Object 。 傳回值之前, GetValue 請先檢查使用者是否具有訪問許可權。

注意

完全信任的程式代碼會忽略存取限制。 也就是說,每當程式代碼完全信任時,都可以透過反映來存取和叫用私人建構函式、方法、字段和屬性。

注意

從 .NET Framework 2.0 Service Pack 1 開始,如果呼叫端已使用 旗標授ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess與呼叫端,而且非公用成員的授與集限制為呼叫端的授與集,或是子集,則這個方法可用來存取非公用成員。 (請參閱 反映的安全性考慮。)

若要使用這項功能,您的應用程式應以 .NET Framework 3.5 或更新版本為目標。

適用於

另請參閱