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 です。

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

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

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

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

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

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

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

次の例では、型のフィールドを表す オブジェクトの FieldInfo 配列を FieldsClass 取得し、 を呼び出 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 ユーザーがアクセス許可を持っているかどうかを確認します。

Note

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

Note

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

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

適用対象

こちらもご覧ください