Type.GetFields 方法

定義

取得目前 Type 的欄位。

多載

GetFields()

傳回目前 Type 的所有公用欄位。

GetFields(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的欄位。

GetFields()

傳回目前 Type 的所有公用欄位。

public:
 cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
public:
 virtual cli::array <System::Reflection::FieldInfo ^> ^ GetFields();
public System.Reflection.FieldInfo[] GetFields ();
member this.GetFields : unit -> System.Reflection.FieldInfo[]
abstract member GetFields : unit -> System.Reflection.FieldInfo[]
override this.GetFields : unit -> System.Reflection.FieldInfo[]
Public Function GetFields () As FieldInfo()

傳回

FieldInfo[]

FieldInfo 物件的陣列,代表為目前 Type 定義的所有公用欄位。

-或- FieldInfo 類型的空陣列 (如果沒有為目前 Type 定義公用欄位)。

實作

範例

下列範例示範如何使用 GetFields() 方法。

#using <system.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel::Design;

int main()
{
   try
   {
      // Get the type handle of a specified class.
      Type^ myType = ViewTechnology::typeid;

      // Get the fields of the specified class.
      array<FieldInfo^>^myField = myType->GetFields();
      Console::WriteLine( "\nDisplaying fields that have SpecialName attributes:\n" );
      for ( int i = 0; i < myField->Length; i++ )
      {
         // Determine whether or not each field is a special name.
         if ( myField[ i ]->IsSpecialName )
         {
            Console::WriteLine( "The field {0} has a SpecialName attribute.", myField[ i ]->Name );
         }
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0} ", e->Message );
   }
}
using System;
using System.Reflection;
using System.ComponentModel.Design;

class FieldInfo_IsSpecialName
{
    public static void Main()
    {
        try
        {
            // Get the type handle of a specified class.
            Type myType = typeof(ViewTechnology);

            // Get the fields of the specified class.
            FieldInfo[] myField = myType.GetFields();

            Console.WriteLine("\nDisplaying fields that have SpecialName attributes:\n");
            for(int i = 0; i < myField.Length; i++)
            {
                // Determine whether or not each field is a special name.
                if(myField[i].IsSpecialName)
                {
                    Console.WriteLine("The field {0} has a SpecialName attribute.",
                        myField[i].Name);
                }
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0} " , e.Message);
        }
    }
}
Imports System.Reflection
Imports System.ComponentModel.Design

Class FieldInfo_IsSpecialName

    Public Shared Sub Main()
        Try
            ' Get the type handle of a specified class.
            Dim myType As Type = GetType(ViewTechnology)

            ' Get the fields of a specified class.
            Dim myField As FieldInfo() = myType.GetFields()

            Console.WriteLine(ControlChars.Cr + "Displaying fields that have SpecialName attributes:" + ControlChars.Cr)
            Dim i As Integer
            For i = 0 To myField.Length - 1
                ' Determine whether or not each field is a special name.
                If myField(i).IsSpecialName Then
                    Console.WriteLine("The field {0} has a SpecialName attribute.", myField(i).Name)
                End If
            Next i
        Catch e As Exception
            Console.WriteLine("Exception : {0} ", e.Message.ToString())
        End Try
    End Sub
End Class

備註

GetFields方法不會以特定順序傳回欄位,例如字母順序或宣告順序。 您的程式碼不得相依于傳回欄位的順序,因為該順序會有所不同。

下表顯示在反映型別時,方法會傳回基類的成員 Get

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱和簽章進行隱藏。
事件 不適用 一般類型系統規則是,繼承與實作為屬性的方法相同。 反映會將屬性視為隱藏名稱和簽章。 請參閱下面的附注2。
方法 可以。 (虛擬和非虛擬) 的方法都可以依名稱或依名稱、依名稱和簽章來隱藏。
巢狀型別
屬性 不適用 一般類型系統規則是,繼承與實作為屬性的方法相同。 反映會將屬性視為隱藏名稱和簽章。 請參閱下面的附注2。
  1. 依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 sentinel 和非受控呼叫慣例。 這是二進位比較。

  2. 針對反映,屬性和事件會依名稱和簽章來隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類的 setter。

  3. 自訂屬性不是一般型別系統的一部分。

如果目前的 Type 代表結構化泛型型別,這個方法會傳回 FieldInfo 類型參數取代為適當類型引數的物件。

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的公用欄位。

另請參閱

適用於

GetFields(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋定義給目前 Type 的欄位。

public:
 abstract cli::array <System::Reflection::FieldInfo ^> ^ GetFields(System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.FieldInfo[] GetFields (System.Reflection.BindingFlags bindingAttr);
abstract member GetFields : System.Reflection.BindingFlags -> System.Reflection.FieldInfo[]
Public MustOverride Function GetFields (bindingAttr As BindingFlags) As FieldInfo()

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回空陣列的 Default

傳回

FieldInfo[]

FieldInfo 物件的陣列,代表為目前 Type 定義之符合指定繫結條件約束的所有欄位。

-或- 如果沒有任何欄位定義給目前 FieldInfo 的欄位,或沒有已定義的欄位符合繫結條件約束,則為 Type 類型的空陣列。

實作

範例

下列範例示範如何使用 GetFields(BindingFlags) 方法。

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
public ref class AttributesSample
{
public:
   void Mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m )
   {
       *str2m = "in Mymethod";
   }
};

void PrintAttributes( Type^ attribType, int iAttribValue )
{
   if (  !attribType->IsEnum )
   {
      Console::WriteLine( "This type is not an enum." );
      return;
   }

   array<FieldInfo^>^fields = attribType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static) );
   for ( int i = 0; i < fields->Length; i++ )
   {
      int fieldvalue = safe_cast<Int32>(fields[ i ]->GetValue( nullptr ));
      if ( (fieldvalue & iAttribValue) == fieldvalue )
      {
         Console::WriteLine( fields[ i ]->Name );
      }
   }
}

int main()
{
   Console::WriteLine( "Reflection.MethodBase.Attributes Sample" );

   // Get the type.
   Type^ MyType = Type::GetType( "AttributesSample" );

   // Get the method Mymethod on the type.
   MethodBase^ Mymethodbase = MyType->GetMethod( "Mymethod" );

   // Display the method name.
   Console::WriteLine( "Mymethodbase = {0}", Mymethodbase );

   // Get the MethodAttribute enumerated value.
   MethodAttributes Myattributes = Mymethodbase->Attributes;

   // Display the flags that are set.
   PrintAttributes( System::Reflection::MethodAttributes::typeid, (int)Myattributes );
   return 0;
}

using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static int Main(string[] args)
    {
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");

        // Get the type.
        Type MyType = Type.GetType("AttributesSample");

        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;

        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }

    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        {
            Console.WriteLine("This type is not an enum.");
            return;
        }

        FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
        for (int i = 0; i < fields.Length; i++)
        {
            int fieldvalue = (int)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}
Imports System.Reflection

Class AttributesSample

    Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
        str2m = "in Mymethod"
    End Sub

    Public Shared Function Main(ByVal args() As String) As Integer
        Console.WriteLine("Reflection.MethodBase.Attributes Sample")

        ' Get the type.
        Dim MyType As Type = Type.GetType("AttributesSample")

        ' Get the method Mymethod on the type.
        Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod")

        ' Display the method name.
        Console.WriteLine("Mymethodbase = {0}.", Mymethodbase)

        ' Get the MethodAttribute enumerated value.
        Dim Myattributes As MethodAttributes = Mymethodbase.Attributes

        ' Display the flags that are set.
        PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
        Return 0
    End Function 'Main

    Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
        If Not attribType.IsEnum Then
            Console.WriteLine("This type is not an enum.")
            Return
        End If
        Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
        Dim i As Integer
        For i = 0 To fields.Length - 1
            Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
            If (fieldvalue And iAttribValue) = fieldvalue Then
                Console.WriteLine(fields(i).Name)
            End If
        Next i
    End Sub
End Class

備註

GetFields(BindingFlags)若要讓多載成功抓取屬性資訊, bindingAttr 引數必須包含至少一個 BindingFlags.InstanceBindingFlags.Static ,以及至少一個 BindingFlags.NonPublicBindingFlags.Public

BindingFlags 可以使用下列篩選旗標來定義要包含在搜尋中的欄位:

  • 指定 BindingFlags.Instance 要包含實例方法。

  • 指定 BindingFlags.Static 要包含靜態方法。

  • 指定 BindingFlags.Public 要在搜尋中包含公用欄位。

  • 指定 BindingFlags.NonPublic 要在搜尋中包含 (的非公用欄位,也就是私用、內部和受保護欄位) 。 只會傳回基類上的受保護和內部欄位;不會傳回基底類別上的私用欄位。

  • 指定 BindingFlags.FlattenHierarchypublic 在階層中包含和 protected 靜態成員; private 不包含繼承類別中的靜態成員。

  • BindingFlags.Default單獨指定以傳回空 PropertyInfo 陣列。

BindingFlags 可以使用下列修飾詞旗標來變更搜尋的運作方式:

  • BindingFlags.DeclaredOnly 只搜尋在上宣告的欄位 Type ,而不是單純繼承的欄位。

如需相關資訊,請參閱 System.Reflection.BindingFlags

GetFields方法不會以特定順序傳回欄位,例如字母順序或宣告順序。 您的程式碼不得相依于傳回欄位的順序,因為該順序會有所不同。

如果目前的 Type 代表結構化泛型型別,這個方法會傳回 FieldInfo 類型參數取代為適當類型引數的物件。

如果目前 Type 表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的公用欄位。

另請參閱

適用於