Type.GetProperties Type.GetProperties Type.GetProperties Type.GetProperties Method

定義

取得目前 Type 的屬性。Gets the properties of the current Type.

多載

GetProperties() GetProperties() GetProperties() GetProperties()

傳回目前 Type 的所有公用屬性。Returns all the public properties of the current Type.

GetProperties(BindingFlags) GetProperties(BindingFlags) GetProperties(BindingFlags) GetProperties(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋目前 Type 的屬性。When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints.

GetProperties() GetProperties() GetProperties() GetProperties()

傳回目前 Type 的所有公用屬性。Returns all the public properties of the current Type.

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

傳回

PropertyInfo 物件的陣列,代表目前 Type 的所有公用屬性。An array of PropertyInfo objects representing all public properties of the current Type.

-或--or- 類型 PropertyInfo 的空陣列,如果目前 Type 並沒有公用屬性。An empty array of type PropertyInfo, if the current Type does not have public properties.

實作

範例

下列範例示範 GetProperties 方法的用法。The following example demonstrates the use of the GetProperties method.

array<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 ] );

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

備註

呼叫這個多載就相當於呼叫GetProperties(BindingFlags)多載bindingAttr引數等於BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public在 C# 和BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.PublicVisual Basic 中。Calling this overload is equivalent to calling the GetProperties(BindingFlags) overload with a bindingAttr argument equal to BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public in C# and BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public in Visual Basic. 它會傳回所有公用執行個體和靜態屬性,表示由目前類型所定義這兩個所Type物件,以及繼承自其基底類型。It returns all public instance and static properties, both those defined by the type represented by the current Type object as well as those inherited from its base types.

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

GetProperties方法沒有傳回屬性以特定順序,例如依字母順序或依宣告順序。The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. 您的程式碼不得相依於屬性會傳回的順序,因為順序不同。Your code must not depend on the order in which properties are returned, because that order varies.

下表顯示基底類別成員會傳回由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.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數的物件。If the current Type represents a constructed generic type, this method returns the PropertyInfo objects 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 properties of the class constraint.

另請參閱

GetProperties(BindingFlags) GetProperties(BindingFlags) GetProperties(BindingFlags) GetProperties(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋目前 Type 的屬性。When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints.

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

參數

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

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

-或--or-

要傳回空陣列的 DefaultDefault to return an empty array.

傳回

物件的陣列,代表目前 Type 中符合指定繫結條件約束的所有屬性。An array of objects representing all properties of the current Type that match the specified binding constraints.

-或--or- 如果目前 PropertyInfo 沒有屬性,或沒有屬性符合繫結條件約束,則為 Type 類型的空陣列。An empty array of type PropertyInfo, if the current Type does not have properties, or if none of the properties match the binding constraints.

實作

範例

下列範例會定義名為的類別PropertyClass包含六個屬性: 兩個都是公用、 其中一個是私用、 一個受保護的、 有內部 (Friend Visual Basic 中),且其中一個受保護內部 (Protected Friend Visual Basic 中)。The following example defines a class named PropertyClass that includes six properties: two are public, one is private, one is protected, one is internal (Friend in Visual Basic), and one is protected internal (Protected Friend in Visual Basic). 接著會顯示一些基本的屬性資訊 (屬性名稱和型別,是否讀取/寫入,以及是否可看到其getset存取子) 符合指定的繫結條件約束的屬性。It then displays some basic property information (the property name and type, whether it is read/write, and the visibility of its get and set accessors) for the properties that match the specified binding constraints.

using namespace System;
using namespace System::Reflection;

// Create a class having three properties.
public ref class PropertyClass
{

public:
   property String^ Property1
   {
      String^ get()
      {
         return "hello";
      }
   }

   property String^ Property2 
   {
      String^ get()
      {
         return "hello";
      }
   }

protected:
   property String^ Property3
   {
      String^ get()
      {
         return "hello";
      }
   }

private:
   property int Property4
   {
      int get()
      {
         return 32;
      }
   }

internal:
   property String^ Property5
   {
      String^ get()
      {
         return "value";
      }
   }
   
public protected:
   property String^ Property6
   {
      String^ get()
      {
         return "value";
      }
   }
};

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

void DisplayPropertyInfo(array<PropertyInfo^>^ propInfos )
{
   // Display information for all properties.
   for each(PropertyInfo^ 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();
   }
}

void main()
{
   Type^ myType = PropertyClass::typeid;
   
   // Get the public properties.
   array<PropertyInfo^>^propInfos = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
   Console::WriteLine("The number of public properties: {0}.\n",
                      propInfos->Length);
   // Display the public properties.
   DisplayPropertyInfo( propInfos );
   
   // Get the non-public properties.
   array<PropertyInfo^>^propInfos1 = myType->GetProperties( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine("The number of non-public properties: {0}.\n",
                      propInfos1->Length);
   // Display all the non-public properties.
   DisplayPropertyInfo(propInfos1);
}
// The example displays the following output:
//       The number of public properties: 2.
//
//          Property name: Property2
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//          Property name: Property1
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Public
//
//       The number of non-public properties: 4.
//
//          Property name: Property6
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected Internal/Friend
//
//          Property name: Property5
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Internal/Friend
//
//          Property name: Property4
//          Property type: System.Int32
//          Read-Write:    False
//          Visibility:    Private
//
//          Property name: Property3
//          Property type: System.String
//          Read-Write:    False
//          Visibility:    Protected
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
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.PublicFor the GetProperties(BindingFlags) overload to successfully retrieve property information, the bindingAttr argument must include at least one of BindingFlags.Instance and BindingFlags.Static, along with at least one of BindingFlags.NonPublic and BindingFlags.Public.

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

  • 指定BindingFlags.Instance包括執行個體方法。Specify BindingFlags.Instance to include instance methods.

  • 指定BindingFlags.Static包含靜態方法。Specify BindingFlags.Static to include static methods.

  • 指定BindingFlags.Public来包含在搜尋中的公用屬性。Specify BindingFlags.Public to include public properties in the search. 屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public.

  • 指定BindingFlags.NonPublic要在搜尋中包含非公用屬性 (也就是私用、 內部和受保護的屬性)。Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search. 只有受保護,而且會傳回基底類別上的內部屬性;不會傳回基底類別上的私用屬性。Only protected and internal properties on base classes are returned; private properties on base classes are not returned.

  • 指定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.Default來傳回空PropertyInfo陣列。Specify BindingFlags.Default alone to return an empty PropertyInfo array.

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

  • BindingFlags.DeclaredOnly 搜尋僅屬性宣告上Type,不只是繼承的屬性。BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.

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

GetProperties方法沒有傳回屬性以特定順序,例如依字母順序或依宣告順序。The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order. 您的程式碼不得相依於屬性會傳回的順序,因為順序不同。Your code must not depend on the order in which properties are returned, because that order varies.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數的物件。If the current Type represents a constructed generic type, this method returns the PropertyInfo objects 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 properties of the class constraint.

另請參閱

適用於