Type.GetField メソッド
現在の Type の特定のフィールドを取得します。
オーバーロードの一覧
指定した名前のフィールドを検索します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public Function GetField(String) As FieldInfo
指定したバインディング制約を使用して、指定したフィールドを検索します。
.NET Compact Framework でもサポート。
[Visual Basic] Overloads Public MustOverride Function GetField(String, BindingFlags) As FieldInfo Implements IReflect.GetField
[C#] public abstract FieldInfo GetField(string, BindingFlags);
[C++] public: virtual FieldInfo* GetField(String*, BindingFlags) = 0;
[JScript] public abstract function GetField(String, BindingFlags) : FieldInfo;
使用例
[Visual Basic, C#, C++] 指定したクラスの Type オブジェクトを取得し、指定したバインディング フラグに一致するフィールドの FieldInfo オブジェクトを取得して、そのフィールドの値を表示する例を次に示します。
[Visual Basic, C#, C++] メモ ここでは、GetField のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic
Public Class MyFieldClassA
Public myField As String = "A Field"
Public Property Field() As String
Get
Return myField
End Get
Set(ByVal Value As String)
If myField <> value Then
myField = value
End If
End Set
End Property
End Class 'MyFieldClassA
Public Class MyFieldClassB
Public myField As String = "B Field"
Public Property Field() As String
Get
Return myField
End Get
Set(ByVal Value As String)
If myField <> value Then
myField = value
End If
End Set
End Property
End Class 'MyFieldClassB
Public Class MyFieldInfoClass
Public Shared Sub Main()
Try
Dim myFieldObjectB As New MyFieldClassB()
Dim myFieldObjectA As New MyFieldClassA()
Dim myTypeA As Type = Type.GetType("MyFieldClassA")
Dim myFieldInfo As FieldInfo = myTypeA.GetField("myField")
Dim myTypeB As Type = Type.GetType("MyFieldClassB")
Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", BindingFlags.Public Or BindingFlags.Instance)
Console.WriteLine("The value of the field is : {0} ", myFieldInfo.GetValue(myFieldObjectA))
Console.WriteLine("The value of the field is : {0} ", myFieldInfo1.GetValue(myFieldObjectB))
Catch e As SecurityException
Console.WriteLine("An exception has occurred: ")
Console.WriteLine(("Message :" + e.Message))
Catch e As ArgumentNullException
Console.WriteLine("An exception has occurred: ")
Console.WriteLine(("Message :" + e.Message))
Catch e As Exception
Console.WriteLine("An exception has occurred: ")
Console.WriteLine(("Message :" + e.Message))
End Try
End Sub 'Main
End Class 'MyFieldInfoClass
[C#]
using System;
using System.Reflection;
using System.Security;
public class MyFieldClassA
{
public string field = "A Field";
public string Field
{
get
{
return field;
}
set
{
if(field!=value)
{
field=value;
}
}
}
}
public class MyFieldClassB
{
public string field = "B Field";
public string Field
{
get
{
return field;
}
set
{
if(field!=value)
{
field=value;
}
}
}
}
public class MyFieldInfoClass
{
public static void Main()
{
try
{
MyFieldClassB myFieldObjectB = new MyFieldClassB();
MyFieldClassA myFieldObjectA = new MyFieldClassA();
Type myTypeA = Type.GetType("MyFieldClassA");
FieldInfo myFieldInfo = myTypeA.GetField("field");
Type myTypeB = Type.GetType("MyFieldClassB");
FieldInfo myFieldInfo1 = myTypeB.GetField("field", BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("The value of the field is : {0} ", myFieldInfo.GetValue(myFieldObjectA));
Console.WriteLine("The value of the field is : {0} ", myFieldInfo1.GetValue(myFieldObjectB));
}
catch(SecurityException e)
{
Console.WriteLine("Exception Raised!");
Console.WriteLine("Message :"+e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("Exception Raised!");
Console.WriteLine("Message :"+e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception Raised!");
Console.WriteLine("Message :"+e.Message);
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public __gc class MyFieldClassA {
public:
String* field;
MyFieldClassA(){
field = S"A Field";
}
__property String* get_Field() {
return field;
}
__property void set_Field(String* value) {
if (field!=value) {
field=value;
}
}
};
public __gc class MyFieldClassB {
public:
String* field;
MyFieldClassB() {
field = S"B Field";
}
__property String* get_Field() {
return field;
}
__property void set_Field(String* value) {
if (field!=value) {
field=value;
}
}
};
int main() {
try {
MyFieldClassB* myFieldObjectB = new MyFieldClassB();
MyFieldClassA* myFieldObjectA = new MyFieldClassA();
Type* myTypeA = Type::GetType(S"MyFieldClassA");
FieldInfo* myFieldInfo = myTypeA->GetField(S"field");
Type* myTypeB = Type::GetType(S"MyFieldClassB");
FieldInfo* myFieldInfo1 = myTypeB->GetField(S"field", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
Console::WriteLine(S"The value of the field is : {0} ", myFieldInfo->GetValue(myFieldObjectA));
Console::WriteLine(S"The value of the field is : {0} ", myFieldInfo1->GetValue(myFieldObjectB));
} catch (SecurityException* e) {
Console::WriteLine(S"Exception Raised!");
Console::WriteLine(S"Message : {0}", e->Message);
} catch (ArgumentNullException* e) {
Console::WriteLine(S"Exception Raised!");
Console::WriteLine(S"Message : {0}", e->Message);
} catch (Exception* e) {
Console::WriteLine(S"Exception Raised!");
Console::WriteLine(S"Message : {0}", e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。