Type.GetProperties 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type属性。
重载
| 名称 | 说明 |
|---|---|
| GetProperties() |
返回当前 Type的所有公共属性。 |
| GetProperties(BindingFlags) |
在派生类中重写时,使用指定的绑定约束搜索当前 Type的属性。 |
GetProperties()
- Source:
- Type.cs
- Source:
- Type.cs
- 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();
[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
注解
调用此重载等效于调用 GetProperties(BindingFlags) 参数等于 C# 中的 bindingAttr 重载,在 Visual Basic 中调用 BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public 等于 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 | 非静态 |
|---|---|---|
| 构造函数 | 否 | 否 |
| 领域 | 否 | Yes. 字段始终是按名称和签名隐藏的。 |
| 事件 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
| 方法 | 否 | Yes. 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。 |
| 嵌套类型 | 否 | 否 |
| 财产 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。 |
按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。
对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。
自定义属性不是常见类型系统的一部分。
如果当前 Type 表示构造的泛型类型,此方法将返回 PropertyInfo 具有由相应类型参数替换的类型参数的对象。
如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
另请参阅
适用于
GetProperties(BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- 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()
参数
返回
表示与指定绑定约束匹配的当前 Type 所有属性的对象数组。
-或-
类型为空的 PropertyInfo数组(如果当前 Type 没有属性,或者没有属性与绑定约束匹配)。
实现
- 属性
示例
以下示例定义一个名为 PropertyClass 的类,其中包括六个属性:两个是公共的,一个是私有的,一个是受保护的,一个是内部(Visual Basic 中的 Friend),一个是受保护的内部(Visual Basic 中的 Protected Friend)。 然后,它会显示一些基本属性信息(属性名称和类型、是否可读/写,以及其 get 和 set 访问器的可见性),这些属性与指定的绑定约束匹配。
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.Instance,BindingFlags.Static以及至少一个和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[])