Type.GetFields 方法

定義

得到電流 Type的場。

多載

名稱 Description
GetFields()

回傳目前 Type所有公域。

GetFields(BindingFlags)

當在派生類別中覆寫時,會利用指定的綁定約束搜尋目前 Type定義的欄位。

GetFields()

回傳目前 Type所有公域。

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

傳回

一個代表目前 Type為 定義的所有公開欄位的物件陣列FieldInfo

-或-

若目前 未定義公共欄位,Type則為 的FieldInfo空陣列。

實作

範例

以下範例展示了該 GetFields() 方法的使用方式。

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);
        }
    }
}
open System.ComponentModel.Design

try
    // Get the type handle of a specified class.
    let myType = typeof<ViewTechnology>

    // Get the fields of the specified class.
    let myFields = myType.GetFields()

    printfn $"\nDisplaying fields that have SpecialName attributes:\n"
    for field in myFields do
        // Determine whether or not each field is a special name.
        if field.IsSpecialName then
            printfn $"The field {field.Name} has a SpecialName attribute."
with e ->
    printfn $"Exception : {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

備註

在.NET 6及更早版本中,GetFields 方法不會以特定順序回傳欄位,例如字母順序或宣告順序。 你的程式碼不能依賴欄位回傳的順序,因為這個順序會變。 然而,從 .NET 7 開始,排序是根據組合中元資料的排序決定性的。

下表顯示當檢視類型結構時,Get 方法會傳回基類的哪些成員。

成員類型 靜態的 非靜態
Constructor No No
Field No 是的。 欄位總是根據名稱和簽章進行隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 Reflection 將屬性視為以名稱與簽名隱藏的方式。 詳見下方註2。
方法 No 是的。 方法(虛擬和非虛擬)可以是依名稱隱藏或依名稱及簽名隱藏。
巢狀類型 No No
Property 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 Reflection 將屬性視為以名稱與簽名隱藏的方式。 詳見下方註2。
  1. 依名稱和簽章隱藏會考慮簽章的所有部分,包括自訂修飾詞、返回類型、參數類型、標記符號 (sentinels) 和非受控呼叫慣例。 這是二進位比較。

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

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

若 current 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 以回傳一個空陣列。

傳回

一個代表當前所有欄位Type且符合指定綁定約束的物件陣列FieldInfo

-或-

若目前未定義欄位,Type或定義欄位不符合綁定約束,則為空FieldInfo陣列。

實作

範例

以下範例展示了該 GetFields(BindingFlags) 方法的使用方式。


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);
            }
        }
    }
}
open System
open System.Reflection

type AttributesSample() =
    member _.Mymethod(int1m: int, str2m: string outref, str3m: string byref) =
        str2m <- "in Mymethod"

let printAttributes (attribType: Type) iAttribValue =
    if not attribType.IsEnum then
        printfn "This type is not an enum."
    else
        let fields = attribType.GetFields(BindingFlags.Public ||| BindingFlags.Static)
        for f in fields do
            let fieldvalue = f.GetValue null :?> int
            if fieldvalue &&& iAttribValue = fieldvalue then
                printfn $"{f.Name}"

printfn "Reflection.MethodBase.Attributes Sample"

// Get the type.
let MyType = Type.GetType "AttributesSample"

// Get the method Mymethod on the type.
let Mymethodbase = MyType.GetMethod "Mymethod"

// Display the method name.
printfn $"Mymethodbase = {Mymethodbase}"

// Get the MethodAttribute enumerated value.
let Myattributes = Mymethodbase.Attributes

// Display the flags that are set.
printAttributes typeof<MethodAttributes> (int Myattributes)
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.FlattenHierarchy 以在階層中包含 publicprotected 的靜態成員,但不包括 private 繼承類別中的靜態成員。

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

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

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

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

在.NET 6及更早版本中,GetFields 方法不會以特定順序回傳欄位,例如字母順序或宣告順序。 你的程式碼不能依賴欄位回傳的順序,因為這個順序會變。 然而,從 .NET 7 開始,排序是根據組合中元資料的排序決定性的。

若 current Type 代表構造化的泛型,此方法會回傳 FieldInfo 將型別參數替換為適當型別參數的物件。

若 在 Type 定義一般型別或通用方法中代表型別參數,該方法會搜尋類別約束的公開欄位。

另請參閱

適用於