Type.GetProperties 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type 的属性。
重载
GetProperties() |
返回为当前 Type 的所有公共属性。 |
GetProperties(BindingFlags) |
当在派生类中重写时,使用指定绑定约束,搜索当前 Type 的属性。 |
GetProperties()
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
返回为当前 Type 的所有公共属性。
public:
cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public:
virtual cli::array <System::Reflection::PropertyInfo ^> ^ GetProperties();
public System.Reflection.PropertyInfo[] GetProperties ();
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
方法的用法。
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());
}
// 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# 和 BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public
Visual Basic 中调用GetProperties(BindingFlags)参数等于 BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public
的重载bindingAttr
。 它返回所有公共实例和静态属性,这些属性由当前 Type 对象表示的类型定义,以及从其基类型继承的属性。
如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static Visual Basic 中的 (,使用 Or
) 组合这些值才能获取它。
在 .NET 6 及更早版本中, GetProperties 方法不按特定顺序(如字母顺序或声明顺序)返回属性。 代码不得依赖于返回属性的顺序,因为该顺序会有所不同。 但是,从 .NET 7 开始,根据程序集中的元数据排序,排序是确定性的。
下表显示了在对类型进行反射时,方法返回 Get
的基类成员。
成员类型 | 静态 | 非静态 |
---|---|---|
构造函数 | 否 | 否 |
字段 | 否 | 可以。 字段始终按名称和签名隐藏。 |
事件 | 不适用 | 常见的类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
方法 | 否 | 可以。 (虚拟和非虚拟) 的方法可以是按名称隐藏或按名称和签名隐藏。 |
嵌套类型 | 否 | 否 |
properties | 不适用 | 常见的类型系统规则是继承与实现 属性的方法的继承相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
按名称隐藏和签名考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinels 和非托管调用约定。 这是二进制比较。
为了反射,属性和事件是按名称和签名隐藏的。 如果基类中有一个同时具有 get 和 set 访问器的属性,但派生类只有一个 get 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类上的 setter。
自定义属性不是通用类型系统的一部分。
如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo 对象,其类型参数替换为相应的类型参数。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。
另请参阅
适用于
GetProperties(BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
当在派生类中重写时,使用指定绑定约束,搜索当前 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 类型的空数组。
实现
示例
以下示例定义一个名为 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";
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
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.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 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- BindingFlags
- DefaultBinder
- GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])