次の方法で共有


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

フィールドはリテラルとしてマークされていますが、フィールドには受け入れ可能なリテラル型の 1 つがありません。

呼び出し元には、このフィールドにアクセスする権限がありません。

メソッドは、 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

次の例では、FieldsClass型のフィールドを表すFieldInfo オブジェクトの配列を取得し、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

このメソッドを使用すると、呼び出し元が ReflectionPermission フラグを持つReflectionPermissionFlag.RestrictedMemberAccessを許可されている場合、および非パブリック メンバーの許可セットが呼び出し元の許可セットまたはそのサブセットに制限されている場合に、非パブリック メンバーにアクセスできます。 ( リフレクションのセキュリティに関する考慮事項を参照してください)。

この機能を使用するには、アプリケーションで Framework 3.5 以降.NETターゲットにする必要があります。

適用対象

こちらもご覧ください