Type.GetField Type.GetField Type.GetField Type.GetField Method

定義

取得目前 Type 的特定欄位。Gets a specific field of the current Type.

多載

GetField(String) GetField(String) GetField(String) GetField(String)

搜尋具有指定名稱的公用欄位。Searches for the public field with the specified name.

GetField(String, BindingFlags) GetField(String, BindingFlags) GetField(String, BindingFlags) GetField(String, BindingFlags)

使用指定的繫結條件約束搜尋指定的欄位。Searches for the specified field, using the specified binding constraints.

GetField(String) GetField(String) GetField(String) GetField(String)

搜尋具有指定名稱的公用欄位。Searches for the public field with the specified name.

public:
 virtual System::Reflection::FieldInfo ^ GetField(System::String ^ name);
public System.Reflection.FieldInfo GetField (string name);
abstract member GetField : string -> System.Reflection.FieldInfo
override this.GetField : string -> System.Reflection.FieldInfo
Public Function GetField (name As String) As FieldInfo

參數

name
String String String String

字串,包含要取得的資料欄位的名稱。The string containing the name of the data field to get.

傳回

物件,代表具有指定之名稱的公用欄位 (如有找到);否則為 nullAn object representing the public field with the specified name, if found; otherwise, null.

實作

例外狀況

這個 Type 物件是 TypeBuilder,尚未呼叫其 CreateType() 方法。This Type object is a TypeBuilder whose CreateType() method has not yet been called.

範例

下列範例會取得Type物件指定的類別,取得FieldInfo欄位的物件,並顯示欄位的值。The following example gets the Type object for the specified class, obtains the FieldInfo object for the field, and displays the value of the field.

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));
    }
}

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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

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 'MyFieldClassB


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 'Main

End Class 'MyFieldInfoClass

備註

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體欄位。The search includes public static and public instance fields.

如果目前Type代表建構的泛型型別,這個方法會傳回FieldInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的欄位。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the fields of the class constraint.

另請參閱

GetField(String, BindingFlags) GetField(String, BindingFlags) GetField(String, BindingFlags) GetField(String, BindingFlags)

使用指定的繫結條件約束搜尋指定的欄位。Searches for the specified field, using the specified binding constraints.

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);
abstract member GetField : string * System.Reflection.BindingFlags -> System.Reflection.FieldInfo
Public MustOverride Function GetField (name As String, bindingAttr As BindingFlags) As FieldInfo

參數

name
String String String String

字串,包含要取得的資料欄位的名稱。The string containing the name of the data field to get.

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

傳回

代表符合指定之需求欄位的物件 (如有找到) ,否則為 nullAn object representing the field that matches the specified requirements, if found; otherwise, null.

實作

例外狀況

範例

下列範例會取得Type物件指定的類別,取得FieldInfo符合指定的繫結旗標,並顯示欄位的值之欄位的物件。The following example gets the Type object for the specified class, obtains the FieldInfo object for the field that matches the specified binding flags, and displays the value of the field.

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));
    }
}

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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

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 'MyFieldClassB


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 'Main

End Class 'MyFieldInfoClass

備註

下表顯示基底類別成員會傳回由Get反映型別上時的方法。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法可以隱藏依名稱或依名稱和簽章隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀的類型Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
  1. 依名稱和簽章隱藏會考慮所有組件的簽章,包括自訂修飾詞,傳回類型、 參數類型、 sentinels 和 unmanaged 呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 反映的屬性和事件會依名稱和簽章隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您同時有 get 和 set 存取子的屬性在基底類別中,但衍生的類別有只有 get 存取子,在衍生的類別的屬性會隱藏基底類別屬性中,並不能存取的基底類別的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不屬於一般類型系統。Custom attributes are not part of the common type system.

下列BindingFlags篩選器旗標可以用來定義要包含在搜尋中的欄位:The following BindingFlags filter flags can be used to define which fields to include in the search:

  • 您必須指定BindingFlags.InstanceBindingFlags.Static以取得傳回值。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定BindingFlags.Public搜尋範圍包含公用欄位。Specify BindingFlags.Public to include public fields in the search.

  • 指定BindingFlags.NonPublic要在搜尋中包含非公用欄位 (也就是私用、 內部和受保護欄位)。Specify BindingFlags.NonPublic to include non-public fields (that is, private, internal, and protected fields) in the search.

  • 指定BindingFlags.FlattenHierarchy包含publicprotected階層; 的靜態成員private中繼承類別的靜態成員不包含。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

下列BindingFlags輔助鍵旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.IgnoreCase 忽略大小寫的nameBindingFlags.IgnoreCase to ignore the case of name.

  • BindingFlags.DeclaredOnly 若要搜尋只上宣告的欄位Type,不只是繼承的欄位。BindingFlags.DeclaredOnly to search only the fields declared on the Type, not fields that were simply inherited.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果目前Type代表建構的泛型型別,這個方法會傳回FieldInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的欄位。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the fields of the class constraint.

另請參閱

適用於