Type.GetProperties 方法

定义

获取当前 Type 的属性。Gets the properties of the current Type.

重载

GetProperties()

返回为当前 Type 的所有公共属性。Returns all the public properties of the current Type.

GetProperties(BindingFlags)

当在派生类中重写时,使用指定绑定约束,搜索当前 Type 的属性。When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints.

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使用与 Visual Basic BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public中C# BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public的相同的参数调用重载。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 NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件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 NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
PropertyProperty 不适用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. 按名称和签名隐藏将考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、个 sentinel 和非托管调用约定。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 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。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)

当在派生类中重写时,使用指定绑定约束,搜索当前 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

枚举值的按位组合,这些值指定如何进行搜索。A bitwise combination of the enumeration values that specify how the search is conducted.

-or-

若为 Default,则返回空数组。Default 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一个是专用的,一个是内部的(Protected Friend在 Visual Basic 中),一个受保护(在 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

注解

BindingFlags.NonPublic BindingFlags.Instance BindingFlags.Static BindingFlags.Public为了使bindingAttr重载成功检索属性信息,参数必须包含至少一个和,以及至少一个和中的一个。 GetProperties(BindingFlags)For 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在层次public结构protected中包含和静态成员;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.

另请参阅

适用于