自定义属性不是通用类型系统的一部分。
如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo 对象,其类型参数替换为相应的类型参数。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。
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()
一个对象数组,它表示当前 Type 中与指定的绑定约束匹配的所有属性。
如果当前 PropertyInfo 没有属性,或者如果没有一个属性匹配绑定约束,则为 Type 类型的空数组。
GetProperties(BindingFlags)
GetProperties(BindingFlags)
以下示例定义一个名为 PropertyClass
的类,其中包含六个属性:两个是 public,一个是 private,一个是受保护的,一个是 Visual Basic) 中的内部 Friend
(,一个是 Visual Basic) 中受保护的内部 Protected Friend
(。 然后,它显示一些基本属性信息, (属性名称和类型,无论是读/写,以及其 get
和 set
访问器的可见性) 与指定的绑定约束匹配的属性。
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";
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";
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 =
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"
"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
Return "hello"
End Get
End Property
Public ReadOnly Property Property2() As String
Return "hello"
End Get
End Property
Protected ReadOnly Property Property3() As String
Return "hello"
End Get
End Property
Private ReadOnly Property Property4 As Integer
Return 32
End Get
End Property
Friend ReadOnly Property Property5 As String
Return "value"
End Get
End Property
Protected Friend ReadOnly Property Property6 As String
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()
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"
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.Static的BindingFlags.Instance一个 ,以及 至少一个 BindingFlags.NonPublic 和 BindingFlags.Public。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包括的属性:
指定 BindingFlags.Instance
以包含实例方法。
指定 BindingFlags.Static
以包含静态方法。
指定 BindingFlags.Public
以在搜索中包含公共属性。 如果属性至少有一个公共访问器,则它被视为公共的反射。
指定 BindingFlags.NonPublic
以包括非公共属性 (,即在搜索中) 私有属性、内部属性和受保护属性。 仅返回基类上的受保护和内部属性;不返回基类上的私有属性。
指定要 BindingFlags.FlattenHierarchy
在层次结构中包括 public
和 protected
静态成员; private
继承类中的静态成员不包括在层次结构中。
单独指定 BindingFlags.Default
以返回空 PropertyInfo 数组。
以下 BindingFlags 修饰符标志可用于更改搜索的工作方式:
BindingFlags.DeclaredOnly
,仅搜索 上 Type声明的属性,而不搜索仅继承的属性。
有关更多信息,请参见System.Reflection.BindingFlags。
在 .NET 6 及更早版本中, GetProperties 方法不按特定顺序(如字母顺序或声明顺序)返回属性。 代码不得依赖于返回属性的顺序,因为该顺序会有所不同。 但是,从 .NET 7 开始,根据程序集中的元数据排序,排序是确定性的。
如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo 对象,其类型参数替换为相应的类型参数。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。