英語で読む

次の方法で共有


FieldInfo.GetValue(Object) メソッド

定義

派生クラスによってオーバーライドされた場合、指定したオブジェクトでサポートされているフィールドの値を返します。

C#
public abstract object GetValue(object obj);
C#
public abstract object? GetValue(object? obj);

パラメーター

obj
Object

フィールド値が返されるオブジェクト。

戻り値

このインスタンスがリフレクションするフィールドの値を保持しているオブジェクト。

実装

例外

フィールドは非静的であり、objnull です。

注: .NET for Windows ストア アプリ または ポータブル クラス ライブラリでは、代わりに catch Exception を使用します。

フィールドがリテラルとマークされていますが、フィールドは許可されたリテラル型を持っていません。

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

注: Windows ストア アプリまたはポータブル クラス ライブラリ用の .NET では、代わりに基本クラスの例外 MemberAccessExceptionである をキャッチします。

メソッドは obj のクラスで宣言も継承もされていません。

次の例では、 メソッドを GetValue 使用して静的フィールドの値を取得します。 引数の obj 値は であることに null注意してください。

C#
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

次の例では、型のフィールドを表す オブジェクトの FieldInfo 配列を FieldsClass 取得し、 を呼び出 GetValue してオブジェクトの各フィールドの値を fieldsInst 表示します。

C#
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'

注釈

フィールドが静的の場合、 obj は無視されます。 非静的フィールドの場合は、 obj フィールドを継承または宣言するクラスのインスタンスにする必要があります。 の戻り値の GetValue 型は であることに Object注意してください。 たとえば、フィールドにブール型のプリミティブ値が保持されている場合、適切なブール値を持つ の Object インスタンスが返されます。 値を返す前に、 GetValue ユーザーがアクセス許可を持っているかどうかを確認します。

注意

完全に信頼されたコードでは、アクセス制限は無視されます。 つまり、プライベート コンストラクター、メソッド、フィールド、およびプロパティは、コードが完全に信頼されるたびにリフレクションを介してアクセスおよび呼び出すことができます。

注意

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

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

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください