Type.GetProperties 方法

定義

取得目前 Type 的屬性。

多載

GetProperties()

傳回目前 Type 的所有公用屬性。

GetProperties(BindingFlags)

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

GetProperties()

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

傳回目前 Type 的所有公用屬性。

C#
public System.Reflection.PropertyInfo[] GetProperties();

傳回

PropertyInfo 物件的陣列,代表目前 Type 的所有公用屬性。

-或-

類型 PropertyInfo 的空陣列,如果目前 Type 並沒有公用屬性。

實作

範例

下列範例示範 GetProperties 方法的用法。

C#
PropertyInfo[] myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties();
Console.WriteLine("Properties of System.Type are:");
for (int i = 0; i < myPropertyInfo.Length; i++)
{
    Console.WriteLine(myPropertyInfo[i].ToString());
}

備註

呼叫此多載相當於在 C# 和 BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Visual Basic 中呼叫GetProperties(BindingFlags)自變數等於 BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public 的多載bindingAttr。 它會傳回所有公用實例和靜態屬性,這些屬性都是由目前 Type 物件所代表的類型所定義,以及繼承自其基底類型的類型。

如果屬性至少有一個存取子是公用的,則屬性會被視為公用。 否則,屬性會被視為私用屬性,而且您必須在Visual Basic中使用 BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (,請使用 Or) 來合併值來取得它。

在 .NET 6 和舊版中 GetProperties ,方法不會以特定順序傳回屬性,例如字母順序或宣告順序。 您的程式代碼不得依存於傳回屬性的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的元數據排序來確定。

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

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

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

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

如果目前 Type 代表建構的泛型型別,這個方法會傳回 PropertyInfo 物件,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的屬性。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperties(BindingFlags)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

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

C#
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);

參數

bindingAttr
BindingFlags

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

-或-

要傳回空陣列的 Default

傳回

物件的陣列,代表目前 Type 中符合指定繫結條件約束的所有屬性。

-或-

如果目前 PropertyInfo 沒有屬性,或沒有屬性符合繫結條件約束,則為 Type 類型的空陣列。

實作

範例

下列範例會定義名為 PropertyClass 的類別,其中包含六個屬性:兩個是 public、一個是私用、一個是受保護、一個是 Visual Basic) 中的內部 (Friend ,另一個是 Visual Basic) 中的內部 (Protected Friend 。 然後,它會在屬性名稱和類型 (顯示一些基本屬性資訊,無論是讀取/寫入,以及其 getset 存取子的可見性) 符合指定系結條件約束的屬性。

C#
using System;
using System.Reflection;

// Create a class having six properties.
public class PropertyClass
{
    public String Property1
    {
        get { return "hello"; }
    }

    public String Property2
    {
        get { return "hello"; }
    }

    protected String Property3
    {
        get { return "hello"; }
    }

    private Int32 Property4
    {
        get { return 32; }
    }

    internal String Property5
    {
       get { return "value"; }
    }

    protected internal String Property6
    {
       get { return "value"; }
    }
}

public class Example
{
    public static void Main()
    {
        Type t = typeof(PropertyClass);
        // Get the public properties.
        PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The number of public properties: {0}.\n",
                          propInfos.Length);
        // Display the public properties.
        DisplayPropertyInfo(propInfos);

        // Get the nonpublic properties.
        PropertyInfo[] propInfos1 = t.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of non-public properties: {0}.\n",
                          propInfos1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(propInfos1);
    }

    public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
    {
        // Display information for all properties.
        foreach (var propInfo in propInfos) {
            bool readable = propInfo.CanRead;
            bool writable = propInfo.CanWrite;

            Console.WriteLine("   Property name: {0}", propInfo.Name);
            Console.WriteLine("   Property type: {0}", propInfo.PropertyType);
            Console.WriteLine("   Read-Write:    {0}", readable & writable);
            if (readable) {
               MethodInfo getAccessor = propInfo.GetMethod;
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(getAccessor));
            }
            if (writable) {
               MethodInfo setAccessor = propInfo.SetMethod;
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(setAccessor));
            }
            Console.WriteLine();
        }
    }

    public static String GetVisibility(MethodInfo accessor)
    {
       if (accessor.IsPublic)
          return "Public";
       else if (accessor.IsPrivate)
          return "Private";
       else if (accessor.IsFamily)
          return "Protected";
       else if (accessor.IsAssembly)
          return "Internal/Friend";
       else
          return "Protected Internal/Friend";
    }
}
// The example displays the following output:
//       The number of public properties: 2.
//
//          Property name: Property1
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//          Property name: Property2
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//       The number of non-public properties: 4.
//
//          Property name: Property3
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected
//
//          Property name: Property4
//          Property type: System.Int32
//          Read-Write:    False
//          Visibility:    Private
//
//          Property name: Property5
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Internal/Friend
//
//          Property name: Property6
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected Internal/Friend

備註

GetProperties(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 和舊版中 GetProperties ,方法不會以特定順序傳回屬性,例如字母順序或宣告順序。 您的程式代碼不得依存於傳回屬性的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的元數據排序來確定。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 PropertyInfo 物件,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的屬性。

另請參閱

適用於

.NET 10 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1