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 應用程式Portable 類別庫中,請選擇 catch Exception

欄位被標記為文字,但該欄位沒有被接受的文字類型之一。

來電者無權存取此欄位。

注意:在.NET中,對於Windows Store 應用程式Portable Class Library,請選擇基底類別例外,MemberAccessException

此方法既未宣告,也未被 obj類別繼承。

範例

以下範例使用此 GetValue 方法來取得靜態場的值。 請注意,該 obj 參數的值為 null

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

以下範例取得一個代表該FieldInfo類型欄位的物件陣列FieldsClass,然後呼叫 以GetValue顯示該物件每個欄位fieldsInst的值。

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 會檢查使用者是否有存取權限。

Note

完全信任的程式碼會忽略存取限制。 也就是說,只要程式碼完全信任,私有建構子、方法、欄位和屬性都可以透過反射存取與調用。

Note

若呼叫者已獲得ReflectionPermissionReflectionPermissionFlag.RestrictedMemberAccess該旗標,且非公開成員的授權集限制於呼叫者的授權集或其子集,此方法可用來存取非公開成員。 (詳見 安全考量以資反思。)

要使用此功能,您的應用程式應針對 .NET Framework 3.5 或更新版本。

適用於

另請參閱