Type.GetField 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type 的特定字段。
重载
| GetField(String) | 搜索具有指定名称的公共字段。 | 
| GetField(String, BindingFlags) | 使用指定绑定约束搜索指定字段。 | 
GetField(String)
- 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);public System.Reflection.FieldInfo? GetField (string name);public System.Reflection.FieldInfo GetField (string name);member this.GetField : string -> System.Reflection.FieldInfoabstract member GetField : string -> System.Reflection.FieldInfo
override this.GetField : string -> System.Reflection.FieldInfoPublic Function GetField (name As String) As FieldInfo参数
- name
- String
包含要获取的数据字段的名称的字符串。
返回
如找到,则为表示具有指定名称的公共字段的对象;否则为 null。
实现
例外
              name 为 null。
此 Type 对象是尚未调用其 CreateType() 方法的 TypeBuilder。
示例
以下示例获取 Type 指定类的 对象,获取 FieldInfo 字段的 对象,并显示 字段的值。
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyFieldClassA
{
public:
   String^ field;
   MyFieldClassA()
   {
      field = "A Field";
   }
   property String^ Field 
   {
      String^ get()
      {
         return field;
      }
      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }
   }
};
public ref class MyFieldClassB
{
public:
   String^ field;
   MyFieldClassB()
   {
      field = "B Field";
   }
   property String^ Field 
   {
      String^ get()
      {
         return field;
      }
      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }
   }
};
int main()
{
   try
   {
      MyFieldClassB^ myFieldObjectB = gcnew MyFieldClassB;
      MyFieldClassA^ myFieldObjectA = gcnew MyFieldClassA;
      Type^ myTypeA = Type::GetType( "MyFieldClassA" );
      FieldInfo^ myFieldInfo = myTypeA->GetField( "field" );
      Type^ myTypeB = Type::GetType( "MyFieldClassB" );
      FieldInfo^ myFieldInfo1 = myTypeB->GetField( "field", static_cast<BindingFlags>(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 : {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "Exception Raised!" );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Raised!" );
      Console::WriteLine( "Message : {0}", e->Message );
   }
}
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
使用指定绑定约束搜索指定字段。
public:
 abstract System::Reflection::FieldInfo ^ GetField(System::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);abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfoPublic MustOverride Function GetField (name As String, bindingAttr As BindingFlags) As FieldInfo参数
- name
- String
包含要获取的数据字段的名称的字符串。
返回
表示符合指定要求的字段的对象(如果找到的话);否则为 null。
实现
例外
              name 为 null。
示例
以下示例获取 Type 指定类的 对象,获取 FieldInfo 与指定绑定标志匹配的字段的 对象,并显示字段的值。
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyFieldClassA
{
public:
   String^ field;
   MyFieldClassA()
   {
      field = "A Field";
   }
   property String^ Field 
   {
      String^ get()
      {
         return field;
      }
      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }
   }
};
public ref class MyFieldClassB
{
public:
   String^ field;
   MyFieldClassB()
   {
      field = "B Field";
   }
   property String^ Field 
   {
      String^ get()
      {
         return field;
      }
      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }
   }
};
int main()
{
   try
   {
      MyFieldClassB^ myFieldObjectB = gcnew MyFieldClassB;
      MyFieldClassA^ myFieldObjectA = gcnew MyFieldClassA;
      Type^ myTypeA = Type::GetType( "MyFieldClassA" );
      FieldInfo^ myFieldInfo = myTypeA->GetField( "field" );
      Type^ myTypeB = Type::GetType( "MyFieldClassB" );
      FieldInfo^ myFieldInfo1 = myTypeB->GetField( "field", static_cast<BindingFlags>(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 : {0}", e->Message );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "Exception Raised!" );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Raised!" );
      Console::WriteLine( "Message : {0}", e->Message );
   }
}
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 的基类成员。
| 成员类型 | 静态 | 非静态 | 
|---|---|---|
| 构造函数 | 否 | 否 | 
| 字段 | 否 | 可以。 字段始终按名称和签名隐藏。 | 
| 事件 | 不适用 | 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。 | 
| 方法 | 否 | 可以。 (虚拟和非虚拟) 的方法可以是按名称隐藏或按名称和签名隐藏。 | 
| 嵌套类型 | 否 | 否 | 
| properties | 不适用 | 常见类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的注释 2。 | 
- 按名称和签名隐藏考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinels 和非托管调用约定。 这是二进制比较。 
- 对于反射,属性和事件按名称和签名隐藏。 如果在基类中具有同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且您将无法访问基类上的 setter。 
- 自定义属性不是通用类型系统的一部分。 
以下 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 表示泛型类型或泛型方法定义中的类型参数,则此方法将搜索类约束的字段。