Language

Type.GetField メソッド

定義

現在の Typeの特定のフィールドを取得します。

オーバーロード

名前 説明
GetField(String)

指定した名前のパブリック フィールドを検索します。

GetField(String, BindingFlags)

指定したバインド制約を使用して、指定したフィールドを検索します。

GetField(String)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定した名前のパブリック フィールドを検索します。

public:
 System::Reflection::FieldInfo ^ GetField(System::String ^ name);
public:
 virtual System::Reflection::FieldInfo ^ GetField(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public System.Reflection.FieldInfo? GetField(string name);
public System.Reflection.FieldInfo GetField(string name);
public System.Reflection.FieldInfo? GetField(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
member this.GetField : string -> System.Reflection.FieldInfo
member this.GetField : string -> System.Reflection.FieldInfo
abstract member GetField : string -> System.Reflection.FieldInfo
override this.GetField : string -> System.Reflection.FieldInfo
Public Function GetField (name As String) As FieldInfo

パラメーター

name
String

取得するデータ フィールドの名前を含む文字列。

返品

指定した名前のパブリック フィールドを表すオブジェクト (見つかった場合)。それ以外の場合は null。

実装

属性

例外

name は nullです。

このType オブジェクトは、TypeBuilder メソッドがまだ呼び出されていないCreateType()です。

例

次の例では、指定したクラスの Type オブジェクトを取得し、フィールドの FieldInfo オブジェクトを取得し、フィールドの値を表示します。


using System;
using System.Reflection;

public class MyFieldClassA
{
    public string Field = "A Field";
}

public class MyFieldClassB
{
    private string field = "B Field";
    public string Field
    {
        get
        {
            return field;
        }
        set
        {
            if (field!=value)
            {
                field=value;
            }
        }
    }
}

public class MyFieldInfoClass
{
    public static void Main()
    {
        MyFieldClassB myFieldObjectB = new MyFieldClassB();
        MyFieldClassA myFieldObjectA = new MyFieldClassA();

        Type myTypeA = typeof(MyFieldClassA);
        FieldInfo myFieldInfo = myTypeA.GetField("Field");

        Type myTypeB = typeof(MyFieldClassB);
        FieldInfo myFieldInfo1 = myTypeB.GetField("field",
            BindingFlags.NonPublic | BindingFlags.Instance);

        Console.WriteLine("The value of the public field is: '{0}'",
            myFieldInfo.GetValue(myFieldObjectA));
        Console.WriteLine("The value of the private field is: '{0}'",
            myFieldInfo1.GetValue(myFieldObjectB));
    }
}
open System.Reflection

type MyFieldClassA =
    val public Field: string
    new () = { Field = "A Field"}

type MyFieldClassB() =
    let field = "B Field"
    member _.Field
        with get () = field

let myFieldObjectB = MyFieldClassB()
let myFieldObjectA = MyFieldClassA()

let myTypeA = typeof<MyFieldClassA>
let myFieldInfo = myTypeA.GetField "Field"

let myTypeB = typeof<MyFieldClassB>
let myFieldInfo1 = myTypeB.GetField("field", BindingFlags.NonPublic ||| BindingFlags.Instance)

printfn $"The value of the public field is: '{myFieldInfo.GetValue myFieldObjectA}'"
printfn $"The value of the private field is: '{myFieldInfo1.GetValue myFieldObjectB}'"

Imports System.Reflection

Public Class MyFieldClassA
    Public Field As String = "A Field"
End Class

Public Class MyFieldClassB
    Private 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


Public Class MyFieldInfoClass

    Public Shared Sub Main()
        Dim myFieldObjectB As New MyFieldClassB()
        Dim myFieldObjectA As New MyFieldClassA()

        Dim myTypeA As Type = GetType(MyFieldClassA)
        Dim myFieldInfo As FieldInfo = myTypeA.GetField("Field")

        Dim myTypeB As Type = GetType(MyFieldClassB)
        Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", _
            BindingFlags.NonPublic Or BindingFlags.Instance)

        Console.WriteLine("The value of the public field is: '{0}'", _
            myFieldInfo.GetValue(myFieldObjectA))
        Console.WriteLine("The value of the private field is: '{0}'", _
            myFieldInfo1.GetValue(myFieldObjectB))
    End Sub

End Class

注釈

nameの検索では、大文字と小文字が区別されます。 検索には、パブリック静的およびパブリック インスタンス フィールドが含まれます。

現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで FieldInfo を返します。

現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のフィールドを検索します。

こちらもご覧ください

適用対象

GetField(String, BindingFlags)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したバインド制約を使用して、指定したフィールドを検索します。

public:
 abstract System::Reflection::FieldInfo ^ GetField(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)]
public abstract System.Reflection.FieldInfo? GetField(string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo GetField(string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo? GetField(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)>]
abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
Public MustOverride Function GetField (name As String, bindingAttr As BindingFlags) As FieldInfo

パラメーター

name
String

取得するデータ フィールドの名前を含む文字列。

bindingAttr
BindingFlags

検索の実行方法を指定する列挙値のビットごとの組み合わせ。

-又は-

Default nullを返します。

返品

指定した要件に一致するフィールドを表すオブジェクト (見つかった場合)。それ以外の場合は null。

実装

属性

例外

name は nullです。

例

次の例では、指定したクラスの Type オブジェクトを取得し、指定したバインド フラグに一致するフィールドの FieldInfo オブジェクトを取得し、フィールドの値を表示します。


using System;
using System.Reflection;

public class MyFieldClassA
{
    public string Field = "A Field";
}

public class MyFieldClassB
{
    private string field = "B Field";
    public string Field
    {
        get
        {
            return field;
        }
        set
        {
            if (field!=value)
            {
                field=value;
            }
        }
    }
}

public class MyFieldInfoClass
{
    public static void Main()
    {
        MyFieldClassB myFieldObjectB = new MyFieldClassB();
        MyFieldClassA myFieldObjectA = new MyFieldClassA();

        Type myTypeA = typeof(MyFieldClassA);
        FieldInfo myFieldInfo = myTypeA.GetField("Field");

        Type myTypeB = typeof(MyFieldClassB);
        FieldInfo myFieldInfo1 = myTypeB.GetField("field",
            BindingFlags.NonPublic | BindingFlags.Instance);

        Console.WriteLine("The value of the public field is: '{0}'",
            myFieldInfo.GetValue(myFieldObjectA));
        Console.WriteLine("The value of the private field is: '{0}'",
            myFieldInfo1.GetValue(myFieldObjectB));
    }
}
open System.Reflection

type MyFieldClassA =
    val public Field: string
    new () = { Field = "A Field"}

type MyFieldClassB() =
    let field = "B Field"
    member _.Field
        with get () = field

let myFieldObjectB = MyFieldClassB()
let myFieldObjectA = MyFieldClassA()

let myTypeA = typeof<MyFieldClassA>
let myFieldInfo = myTypeA.GetField "Field"

let myTypeB = typeof<MyFieldClassB>
let myFieldInfo1 = myTypeB.GetField("field", BindingFlags.NonPublic ||| BindingFlags.Instance)

printfn $"The value of the public field is: '{myFieldInfo.GetValue myFieldObjectA}'"
printfn $"The value of the private field is: '{myFieldInfo1.GetValue myFieldObjectB}'"

Imports System.Reflection

Public Class MyFieldClassA
    Public Field As String = "A Field"
End Class

Public Class MyFieldClassB
    Private 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


Public Class MyFieldInfoClass

    Public Shared Sub Main()
        Dim myFieldObjectB As New MyFieldClassB()
        Dim myFieldObjectA As New MyFieldClassA()

        Dim myTypeA As Type = GetType(MyFieldClassA)
        Dim myFieldInfo As FieldInfo = myTypeA.GetField("Field")

        Dim myTypeB As Type = GetType(MyFieldClassB)
        Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", _
            BindingFlags.NonPublic Or BindingFlags.Instance)

        Console.WriteLine("The value of the public field is: '{0}'", _
            myFieldInfo.GetValue(myFieldObjectA))
        Console.WriteLine("The value of the private field is: '{0}'", _
            myFieldInfo1.GetValue(myFieldObjectB))
    End Sub

End Class

注釈

次の表は、型に反映するときに、 Get メソッドによって返される基底クラスのメンバーを示しています。

メンバーの種類 Static 非静的
コンストラクター いいえ いいえ
フィールド いいえ Yes. フィールドは常に名前と署名によって非表示になります。
イベント 適用なし 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。
Method いいえ Yes. メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。
入れ子にされた型 いいえ いいえ
財産 適用なし 一般的な型システムルールは、継承がプロパティを実装するメソッドの継承と同じであるということです。 リフレクションは、プロパティを名前と署名による非表示として扱います。 以下の注 2 を参照してください。
  1. 名前と署名による隠蔽は、カスタム修飾子、戻り値の型、パラメーター型、センチネル、そしてアンマネージド呼び出し規則を含む、署名のすべての部分を考慮します。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

次の BindingFlags フィルター フラグを使用して、検索に含めるフィールドを定義できます。

  • 戻り値を取得するには、 BindingFlags.Instance または BindingFlags.Static を指定する必要があります。

  • 検索にパブリック フィールドを含める BindingFlags.Public を指定します。

  • BindingFlags.NonPublicを指定して、非パブリック フィールド (プライベート、内部、および保護されたフィールド) を検索に含めます。

  • BindingFlags.FlattenHierarchyおよびpublic静的メンバーを階層に含めるprotectedを指定します。継承されたクラスprivate静的メンバーは含まれません。

次の BindingFlags 修飾子フラグを使用して、検索の動作を変更できます。

  • BindingFlags.IgnoreCase の大文字と小文字を無視するには name。

  • BindingFlags.DeclaredOnly は、単に継承されたフィールドではなく、 Typeで宣言されているフィールドのみを検索します。

詳細については、System.Reflection.BindingFlags を参照してください。

現在の Type が構築されたジェネリック型を表す場合、このメソッドは、適切な型引数に置き換えられた型パラメーターで FieldInfo を返します。

現在の Type がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表す場合、このメソッドはクラス制約のフィールドを検索します。

こちらもご覧ください

適用対象