语言

Type.GetField 方法

定义

获取当前 Type字段的特定字段。

重载

名称 说明
GetField(String)

搜索具有指定名称的公共字段。

GetField(String, BindingFlags)

使用指定的绑定约束搜索指定字段。

GetField(String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
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)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
Type.cs
Source:
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。
方法 否 Yes. 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。
嵌套类型 否 否
财产 不適用 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。
  1. 按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。

  2. 对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。

  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 表示泛型类型或泛型方法的定义中的类型参数,此方法将搜索类约束的字段。

另请参阅

适用于