FieldInfo.GetValue(Object) メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
派生クラスによってオーバーライドされた場合、指定したオブジェクトでサポートされているフィールドの値を返します。
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
フィールド値が返されるオブジェクト。
このインスタンスがリフレクションするフィールドの値を保持しているオブジェクト。
フィールドは非静的であり、obj
は null
です。
注: .NET for Windows ストア アプリ または ポータブル クラス ライブラリでは、代わりに catch Exception を使用します。
フィールドがリテラルとマークされていますが、フィールドは許可されたリテラル型を持っていません。
呼び出し元には、このフィールドに対するアクセス許可がありません。
注: Windows ストア アプリまたはポータブル クラス ライブラリ用の .NET では、代わりに基本クラスの例外 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
ユーザーがアクセス許可を持っているかどうかを確認します。
注意
完全に信頼されたコードでは、アクセス制限は無視されます。 つまり、プライベート コンストラクター、メソッド、フィールド、およびプロパティは、コードが完全に信頼されるたびにリフレクションを介してアクセスおよび呼び出すことができます。
注意
.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 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。