Type.GetProperties 方法

定義

得到電流 Type的性質。

多載

名稱 Description
GetProperties()

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

GetProperties(BindingFlags)

當在導出類別中覆寫時,會利用指定的綁定約束搜尋當前 Type的屬性。

GetProperties()

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

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

public:
 cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
 virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo[] GetProperties();
public System.Reflection.PropertyInfo[] GetProperties();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
member this.GetProperties : unit -> System.Reflection.PropertyInfo[]
abstract member GetProperties : unit -> System.Reflection.PropertyInfo[]
override this.GetProperties : unit -> System.Reflection.PropertyInfo[]
Public Function GetProperties () As PropertyInfo()

傳回

一個代表當前 PropertyInfo所有公共屬性的物件陣列Type

-或-

PropertyInfo電流沒有公共屬性,則為一個類型的Type空陣列。

實作

屬性

範例

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

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());
}
// Get the properties of 'Type' class object.
let myPropertyInfo = Type.GetType("System.Type").GetProperties()
printfn "Properties of System.Type are:"
for pi in myPropertyInfo do
    printfn $"{pi}"
Dim myPropertyInfo() As PropertyInfo
' Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties()
Console.WriteLine("Properties of System.Type are:")
Dim i As Integer
For i = 0 To myPropertyInfo.Length - 1
   Console.WriteLine(myPropertyInfo(i).ToString())
Next i

備註

呼叫此超載等同於在 C# 中呼叫 GetProperties(BindingFlags) 過載,且參數為 bindingAttrBindingFlags.Instance | BindingFlags.Static | BindingFlags.Public,Visual Basic 中為 BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public。 它回傳所有公開實例與靜態屬性,包括由目前 Type 物件所代表的類型定義的屬性,以及從其基礎型別繼承的屬性。

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

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

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

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

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

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

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

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

另請參閱

適用於

GetProperties(BindingFlags)

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

當在導出類別中覆寫時,會利用指定的綁定約束搜尋當前 Type的屬性。

public:
 abstract cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.PropertyInfo[] GetProperties(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
abstract member GetProperties : System.Reflection.BindingFlags -> System.Reflection.PropertyInfo[]
Public MustOverride Function GetProperties (bindingAttr As BindingFlags) As PropertyInfo()

參數

bindingAttr
BindingFlags

以位元組合列舉值,指定搜尋方式。

-或-

Default 以回傳一個空陣列。

傳回

一個代表所有符合指定綁定約束的當前屬性 Type 的物件陣列。

-或-

PropertyInfo當前的屬性不具,或所有屬性不符合綁定約束,則為 類型為 Type的空陣列。

實作

屬性

範例

以下範例定義了一個名為 的類別,包含六個屬性:兩個是公開的,一個是私有的,一個是保護的,一個是內部的(),以及一個是受保護的內部(< Visual Basic 中的 c2 />)。 接著它會顯示一些基本的屬性資訊(屬性名稱和類型、是否是讀寫,以及其 getset 存取者的可見性),這些屬性符合指定的綁定約束。

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

// Create a class having four properties.
type PropertyClass() =
    member _.Property1 = 
        "hello"

    member _.Property2 = 
        "hello"

    member private _.Property3 = 
        32

    member internal _.Property4 =
       "value"

let getVisibility (accessor: MethodInfo) =
    if accessor.IsPublic then
        "Public"
    elif accessor.IsPrivate then
        "Private"
    elif accessor.IsFamily then
        "Protected"
    elif accessor.IsAssembly then
        "Internal/Friend"
    else
        "Protected Internal/Friend"

let displayPropertyInfo (propInfos: #seq<PropertyInfo>) = 
    // Display information for all properties.
    for propInfo in propInfos do
        let readable = propInfo.CanRead
        let writable = propInfo.CanWrite

        printfn $"   Property name: {propInfo.Name}"
        printfn $"   Property type: {propInfo.PropertyType}"
        printfn $"   Read-Write:    {readable && writable}"
        if readable then
            let getAccessor = propInfo.GetMethod
            printfn $"   Visibility:    {getVisibility getAccessor}"
        if writable then
            let setAccessor = propInfo.SetMethod
            printfn $"   Visibility:    {getVisibility setAccessor}"
        printfn ""

let t = typeof<PropertyClass>
// Get the public properties.
let propInfos = t.GetProperties(BindingFlags.Public ||| BindingFlags.Instance)
printfn $"The number of public properties: {propInfos.Length}.\n"
// Display the public properties.
displayPropertyInfo propInfos

// Get the nonpublic properties.
let propInfos1 = t.GetProperties(BindingFlags.NonPublic ||| BindingFlags.Instance)
printfn $"The number of non-public properties: {propInfos1.Length}.\n"
// Display all the nonpublic properties.
displayPropertyInfo propInfos1

// 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: 2.
//
//          Property name: Property3
//          Property type: System.Int32
//          Read-Write:    False
//          Visibility:    Private
//
//          Property name: Property4
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Internal/Friend
Imports System.Reflection

' Create a class having six properties.
Public Class PropertyClass
    Public ReadOnly Property Property1() As String
        Get
            Return "hello"
        End Get
    End Property

    Public ReadOnly Property Property2() As String
        Get
            Return "hello"
        End Get
    End Property

    Protected ReadOnly Property Property3() As String
        Get
            Return "hello"
        End Get
    End Property

    Private ReadOnly Property Property4 As Integer
        Get
           Return 32
        End Get
    End Property

    Friend ReadOnly Property Property5 As String
       Get
          Return "value"
       End Get
    End Property

    Protected Friend ReadOnly Property Property6 As String
       Get
          Return "value"
       End Get
    End Property
End Class

Public Module Example
    Public Sub Main()
        Dim t As Type = GetType(PropertyClass)
        ' Get the public properties.
        Dim propInfos As PropertyInfo() = t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("The number of public properties: {0}",
                          propInfos.Length)
        Console.WriteLine()
        ' Display the public properties.
        DisplayPropertyInfo(propInfos)

        ' Get the non-public properties.
        Dim propInfos1 As PropertyInfo() = t.GetProperties(BindingFlags.NonPublic Or BindingFlags.Instance)
        Console.WriteLine("The number of non-public properties: {0}",
                          propInfos1.Length)
        Console.WriteLine()
        ' Display all the non-public properties.
        DisplayPropertyInfo(propInfos1)
    End Sub

    Public Sub DisplayPropertyInfo(ByVal propInfos() As PropertyInfo)
        ' Display information for all properties.
        For Each propInfo In propInfos
            Dim readable As Boolean = propInfo.CanRead
            Dim writable As Boolean = propInfo.CanWrite
            
            Console.WriteLine("   Property name: {0}", propInfo.Name)
            Console.WriteLine("   Property type: {0}", propInfo.PropertyType)
            Console.WriteLine("   Read-Write:    {0}", readable And writable)
            If readable Then
               Dim getAccessor As MethodInfo = propInfo.GetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(getAccessor))
            End If
            If writable Then
               Dim setAccessor As MethodInfo = propInfo.SetMethod
               Console.WriteLine("   Visibility:    {0}",
                                 GetVisibility(setAccessor))
            End If
            Console.WriteLine()
        Next
    End Sub
    
    Public Function GetVisibility(accessor As MethodInfo) As String
       If accessor.IsPublic Then
          Return "Public"
       ElseIf accessor.IsPrivate Then
          Return "Private"
       Else If accessor.IsFamily Then
          Return "Protected"
       Else If accessor.IsAssembly Then
          Return "Internal/Friend"
       Else
          Return "Protected Internal/Friend"
       End If
    End Function
End Module
' 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 開始,排序是根據組合中元資料的排序決定性的。

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

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

另請參閱

適用於