Type.GetProperty 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type属性的特定属性。
重载
| 名称 | 说明 |
|---|---|
| GetProperty(String, Type, Type[]) |
搜索其参数与指定参数类型匹配的指定公共属性。 |
| GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) |
使用指定的绑定约束搜索其参数与指定参数类型和修饰符匹配的指定属性。 |
| GetProperty(String, Type, Type[], ParameterModifier[]) |
搜索其参数与指定参数类型和修饰符匹配的指定公共属性。 |
| GetProperty(String, Type[]) |
搜索其参数与指定参数类型匹配的指定公共属性。 |
| GetProperty(String, Type) |
搜索具有指定名称和返回类型的公共属性。 |
| GetProperty(String, BindingFlags) |
使用指定的绑定约束搜索指定的属性。 |
| GetProperty(String) |
搜索具有指定名称的公共属性。 |
GetProperty(String, Type, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索其参数与指定参数类型匹配的指定公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type()) As PropertyInfo
参数
- name
- String
包含要获取的公共属性的名称的字符串。
- returnType
- Type
属性的返回类型。
- types
- Type[]
表示要获取的索引属性的参数数、顺序和类型的对象的数组 Type 。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取未编制索引的属性。
返回
一个对象,表示其参数与指定参数类型匹配的公共属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称并匹配指定参数类型的多个属性。
types 是多维。
的元素为 typesnull.
注解
如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一Or起)来获取该属性。
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
使用指定的绑定约束搜索其参数与指定参数类型和修饰符匹配的指定属性。
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags, binder As Binder, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
参数
- name
- String
包含要获取的属性的名称的字符串。
- binder
- Binder
一个对象,定义一组属性并启用绑定,这可能涉及选择重载的方法、强制参数类型以及通过反射调用成员。
-或-
null 引用(Visual Basic 中的 Nothing),以使用 DefaultBinder。
- returnType
- Type
属性的返回类型。
- types
- Type[]
表示要获取的索引属性的参数数、顺序和类型的对象的数组 Type 。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取未编制索引的属性。
- modifiers
- ParameterModifier[]
表示与数组中ParameterModifier相应元素关联的属性的对象数组types。 默认绑定器不处理此参数。
返回
一个对象,表示符合指定要求的属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称并匹配指定绑定约束的多个属性。
的元素为 typesnull.
注解
以下指南适用于所有重载:
- 如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一
Or起)来获取该属性。 - 如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
- 如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
GetProperty(String) 方法
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
发生 AmbiguousMatchException 的情况包括:
- 类型包含两个具有相同名称但不同数量的参数的索引属性。 若要解决歧义,请使用指定参数类型的 GetProperty 方法的重载。
- 派生类型通过使用
new修饰符(在 Visual Basic 中为Shadows),声明一个属性,该属性会隐藏具有相同名称的继承属性。 若要解决歧义,请使用 GetProperty(String, BindingFlags) 方法重载并添加 BindingFlags.DeclaredOnly 标志以将搜索限制为未继承的成员。
GetProperty(String, BindingFlags) 方法
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些属性:
- 您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。 - 指定
BindingFlags.Public在搜索中包含公共属性。 - 指定
BindingFlags.NonPublic在搜索中包含非公共属性(即专用属性、内部属性和受保护属性)。 - 指定
BindingFlags.FlattenHierarchy为包含public层次结构中的静态成员和protected静态成员;private不包括继承类中的静态成员。
以下 BindingFlags 修饰符标志可用于更改搜索的工作原理:
-
BindingFlags.IgnoreCase用于忽略name的大小写。 -
BindingFlags.DeclaredOnly仅搜索在Type中声明的属性,而不是继承的属性。
发生 AmbiguousMatchException 的情况包括:
- 类型包含两个具有相同名称但不同数量的参数的索引属性。 若要解决歧义,请使用指定参数类型的 GetProperty 方法的重载。
- 派生类型声明一个属性,该属性使用
new修饰符(Shadows在 Visual Basic 中)隐藏具有相同名称的继承属性。 若要解决歧义,请包括 BindingFlags.DeclaredOnly 将搜索限制为未继承的成员。
GetProperty(System.String、System.Reflection.BindingFlags、System.Reflection.Binder、System.Type、System.Type[]、System.Reflection.ParameterModifier[]) 方法
尽管默认联编程序不处理 ParameterModifier ( modifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。
ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。
下表显示了通过 Get 方法在反射类型时返回的基类成员。
| 成员类型 | Static | 非静态 |
|---|---|---|
| 构造函数 | 否 | 否 |
| 领域 | 否 | 是的。 字段始终是按名称和签名隐藏的。 |
| Event | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为通过名称和签名进行隐藏。2 |
| 方法 | 否 | 是的。 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。 |
| 嵌套类型 | 否 | 否 |
| 财产 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为通过名称和签名进行隐藏。2 |
Notes:
- 按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。
- 对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。
- 自定义属性不是常见类型系统的一部分。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些属性:
- 您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。 - 指定
BindingFlags.Public在搜索中包含公共属性。 - 指定
BindingFlags.NonPublic在搜索中包含非公共属性(即专用属性、内部属性和受保护属性)。 - 指定
BindingFlags.FlattenHierarchy为包含public层次结构中的静态成员和protected静态成员;private不包括继承类中的静态成员。
以下 BindingFlags 修饰符标志可用于更改搜索的工作原理:
-
BindingFlags.IgnoreCase用于忽略name的大小写。 -
BindingFlags.DeclaredOnly仅搜索在Type中声明的属性,而不是继承的属性。
索引器和默认属性
Visual Basic、C# 和 C++的语法简化了访问索引属性的语法,并允许一个索引属性作为其类型的默认值。 例如,如果变量 myList 引用了一个 ArrayList,则语法 myList[3] (myList(3) 在 Visual Basic 中)检索索引为 3 的元素。 你可以重载此属性。
在 C# 中,此功能称为索引器,不能按名称引用。 默认情况下,C# 索引器在元数据中显示为名为 Item 的索引属性。 但是,类库开发人员可以使用 IndexerNameAttribute 属性更改元数据中的索引器的名称。 例如,该 String 类具有一个名为 Chars[] 的索引器。 使用 C# 以外的语言创建的索引属性可以具有与 Item 不同的名称。
若要确定类型是否具有默认属性,请使用 GetCustomAttributes(Type, Boolean) 该方法测试 DefaultMemberAttribute 属性。 如果该类型具有 DefaultMemberAttribute,则 MemberName 属性返回默认属性的名称。
另请参阅
- PropertyInfo
- String
- BindingFlags
- Binder
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, Type, Type[], ParameterModifier[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索其参数与指定参数类型和修饰符匹配的指定公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo
参数
- name
- String
包含要获取的公共属性的名称的字符串。
- returnType
- Type
属性的返回类型。
- types
- Type[]
表示要获取的索引属性的参数数、顺序和类型的对象的数组 Type 。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取未编制索引的属性。
- modifiers
- ParameterModifier[]
表示与数组中ParameterModifier相应元素关联的属性的对象数组types。 默认绑定器不处理此参数。
返回
一个对象,表示符合指定要求的公共属性(如果找到);否则,为 null.
实现
- 属性
例外
使用指定的名称和修饰符找到多个属性,并匹配指定的参数类型和修饰符。
的元素为 typesnull.
示例
以下示例获取 Type 与此类对应的 MyPropertyClass对象,并使用传递给 GetProperty 该方法的参数检索此类的索引属性。
using System;
using System.Reflection;
public class MyPropertyClass
{
private readonly int [,] _myPropertyArray = new int[10,10];
// Declare an indexer.
public int this [int i,int j]
{
get
{
return _myPropertyArray[i,j];
}
set
{
_myPropertyArray[i,j] = value;
}
}
}
public class MyTypeClass
{
public static void Main()
{
try
{
Type myType=typeof(MyPropertyClass);
Type[] myTypeArray = new Type[2];
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray.SetValue(typeof(int),0);
myTypeArray.SetValue(typeof(int),1);
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
PropertyInfo myPropertyInfo = myType.GetProperty("Item",
typeof(int),myTypeArray,null);
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
" has a property type of " + myPropertyInfo.PropertyType);
}
catch(Exception ex)
{
Console.WriteLine("An exception occurred " + ex.Message);
}
}
}
open System
type MyPropertyClass() =
let myPropertyArray = Array2D.zeroCreate<int> 10 10
// Declare an indexed property.
member _.Item
with get (i, j) =
myPropertyArray[i, j]
and set (i, j) value =
myPropertyArray[i, j] <- value
try
let myType = typeof<MyPropertyClass>
let myTypeArray = Array.zeroCreate<Type> 2
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray.SetValue(typeof<int>, 0)
myTypeArray.SetValue(typeof<int>, 1)
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
let myPropertyInfo = myType.GetProperty("Item", typeof<int>, myTypeArray, null)
printfn $"{myType.FullName}.{myPropertyInfo.Name} has a property type of {myPropertyInfo.PropertyType}"
with ex ->
printfn $"An exception occurred {ex.Message}"
Imports System.Reflection
Public Class MyPropertyClass
Private myPropertyArray(9, 9) As Integer
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myPropertyArray(i, j)
End Get
Set(ByVal Value As Integer)
myPropertyArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass21
Public Shared Sub Run()
Try
Dim myType As Type = GetType(MyPropertyClass)
Dim myTypeArray(1) As Type
' Create an instance of a Type array representing the number, order
' and type of the parameters for the property.
myTypeArray.SetValue(GetType(Integer), 0)
myTypeArray.SetValue(GetType(Integer), 1)
' Search for the indexed property whose parameters match the
' specified argument types and modifiers.
Dim myPropertyInfo As PropertyInfo = myType.GetProperty("Item",
GetType(Integer), myTypeArray, Nothing)
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
" has a property type of " + myPropertyInfo.PropertyType.ToString())
Catch ex As Exception
Console.WriteLine("An exception occurred " + ex.Message.ToString())
End Try
End Sub
End Class
注解
如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一Or起)来获取该属性。
尽管默认联编程序不处理 ParameterModifier ( modifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。
ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- ParameterModifier
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, Type[])
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索其参数与指定参数类型匹配的指定公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type[] types);
public System.Reflection.PropertyInfo GetProperty(string name, Type[] types);
public System.Reflection.PropertyInfo? GetProperty(string name, Type[] types);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, types As Type()) As PropertyInfo
参数
- name
- String
包含要获取的公共属性的名称的字符串。
- types
- Type[]
表示要获取的索引属性的参数数、顺序和类型的对象的数组 Type 。
-或-
类型(即 Type[] types = new Type[0])的 Type 空数组,用于获取未编制索引的属性。
返回
一个对象,表示其参数与指定参数类型匹配的公共属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称并匹配指定参数类型的多个属性。
types 是多维。
的元素为 typesnull.
示例
下面的示例检索 Type 用户定义的类的对象,检索该类的属性,并显示传递给 GetProperty的参数指定的属性名称和类型。
using System;
using System.Reflection;
class MyClass3
{
private readonly int[,] _myArray = { { 1, 2 }, { 3, 4 } };
// Declare an indexer.
public int this[int i, int j]
{
get
{
return _myArray[i, j];
}
set
{
_myArray[i, j] = value;
}
}
}
public class MyTypeClass3
{
public static void Main(string[] args)
{
try
{
// Get the Type object.
Type myType = typeof(MyClass3);
Type[] myTypeArr = new Type[2];
// Create an instance of a Type array.
myTypeArr.SetValue(typeof(int), 0);
myTypeArr.SetValue(typeof(int), 1);
// Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
// Display the property.
Console.WriteLine("The {0} property exists in MyClass3.",
myPropInfo.ToString());
}
catch (NullReferenceException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Source : {0}", e.Source);
Console.WriteLine("Message : {0}", e.Message);
}
}
}
open System
type MyClass1() =
let myArray = array2D [[1; 2]; [3; 4]]
// Declare an indexed property.
member _.Item
with get (i, j) =
myArray[i, j]
and set (i, j) value =
myArray[i, j] <- value
try
// Get the Type object.
let myType = typeof<MyClass1>
let myTypeArr = Array.zeroCreate<Type> 2
// Create an instance of a Type array.
myTypeArr.SetValue(typeof<int>, 0)
myTypeArr.SetValue(typeof<int>, 1)
// Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
let myPropInfo = myType.GetProperty("Item", myTypeArr)
// Display the property.
printfn $"The {myPropInfo} property exists in MyClass1."
with :? NullReferenceException as e ->
printfn "An exception occurred."
printfn $"Source : {e.Source}"
printfn $"Message : {e.Message}"
Imports System.Reflection
Module Module3
Class MyClass3
Private myArray As Integer(,) = {{1, 2}, {3, 4}}
' Declare an indexer.
Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
Get
Return myArray(i, j)
End Get
Set(ByVal Value As Integer)
myArray(i, j) = Value
End Set
End Property
End Class
Public Class MyTypeClass3
Public Shared Sub Run()
Try
' Get the Type Object.
Dim myType As Type = GetType(MyClass3)
Dim myTypeArr(1) As Type
' Create an instance of a Type array.
myTypeArr.SetValue(GetType(Integer), 0)
myTypeArr.SetValue(GetType(Integer), 1)
' Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
' Display the property.
Console.WriteLine("The {0} property exists in MyClass3.", myPropInfo.ToString())
Catch e As NullReferenceException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module3
注解
如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一Or起)来获取该属性。
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, Type)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索具有指定名称和返回类型的公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType);
public System.Reflection.PropertyInfo GetProperty(string name, Type returnType);
public System.Reflection.PropertyInfo? GetProperty(string name, Type? returnType);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
member this.GetProperty : string * Type -> System.Reflection.PropertyInfo
abstract member GetProperty : string * Type -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type) As PropertyInfo
参数
- name
- String
包含要获取的公共属性的名称的字符串。
- returnType
- Type
属性的返回类型。
返回
一个对象,表示具有指定名称的公共属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称的多个属性。
name 是 null,或 returnType 为 null。
示例
以下示例定义一个具有一个属性的类,并检索属性的名称和类型。
using System;
using System.Reflection;
class MyPropertyTypeClass
{
public string MyProperty1 { get; set; } = "Hello World.";
}
class TestClass
{
static void Main()
{
try
{
Type myType = typeof(MyPropertyTypeClass);
// Get the PropertyInfo object representing MyProperty1.
PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1", typeof(string));
Console.WriteLine("The name of the first property of MyPropertyTypeClass is {0}.",
myStringProperties1.Name);
Console.WriteLine("The type of the first property of MyPropertyTypeClass is {0}.",
myStringProperties1.PropertyType);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException :" + e.Message);
}
catch (AmbiguousMatchException e)
{
Console.WriteLine("AmbiguousMatchException :" + e.Message);
}
catch (NullReferenceException e)
{
Console.WriteLine("Source : {0}", e.Source);
Console.WriteLine("Message : {0}", e.Message);
}
//Output:
//The name of the first property of MyPropertyTypeClass is MyProperty1.
//The type of the first property of MyPropertyTypeClass is System.String.
}
}
open System
open System.Reflection
type MyClass1() =
let mutable myMessage = "Hello World."
member _.MyProperty1
with get () =
myMessage
and set (value) =
myMessage <- value
try
let myType = typeof<MyClass1>
// Get the PropertyInfo object representing MyProperty1.
let myStringProperties1 = myType.GetProperty("MyProperty1", typeof<string>)
printfn $"The name of the first property of MyClass1 is {myStringProperties1.Name}."
printfn $"The type of the first property of MyClass1 is {myStringProperties1.PropertyType}."
with
| :? ArgumentNullException as e ->
printfn $"ArgumentNullException :{e.Message}"
| :? AmbiguousMatchException as e ->
printfn $"AmbiguousMatchException :{e.Message}"
| :? NullReferenceException as e ->
printfn $"Source : {e.Source}"
printfn $"Message : {e.Message}"
// Output:
// The name of the first property of MyClass1 is MyProperty1.
// The type of the first property of MyClass1 is System.String.
Imports System.Reflection
Class MyTypesClass
Private myMessage As [String] = "Hello World."
Public Property MyProperty1() As String
Get
Return myMessage
End Get
Set(ByVal Value As String)
myMessage = Value
End Set
End Property
End Class
Class TestClass
Shared Sub Run()
Try
Dim myType As Type = GetType(MyTypesClass)
' Get the PropertyInfo object representing MyProperty1.
Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))
Console.WriteLine("The name of the first property of MyTypesClass is {0}.", myStringProperties1.Name)
Console.WriteLine("The type of the first property of MyTypesClass is {0}.", myStringProperties1.PropertyType.ToString())
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException :" + e.Message.ToString())
Catch e As AmbiguousMatchException
Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())
Catch e As NullReferenceException
Console.WriteLine("Source : {0}", e.Source.ToString())
Console.WriteLine("Message : {0}", e.Message.ToString())
End Try
'Output:
'The name of the first property of MyTypesClass is MyProperty1.
'The type of the first property of MyTypesClass is System.String.
End Sub
End Class
注解
如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一Or起)来获取该属性。
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, BindingFlags)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
使用指定的绑定约束搜索指定的属性。
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.PropertyInfo? GetProperty(string name, System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags) As PropertyInfo
参数
- name
- String
包含要获取的属性的名称的字符串。
返回
一个对象,表示符合指定要求的属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称并匹配指定绑定约束的多个属性。
name 是 null。
示例
以下示例检索用户定义的类的类型,检索该类的属性,并根据指定的绑定约束显示属性名称。
using System;
using System.Reflection;
class MyClass2
{
// Declare MyProperty.
public int MyProperty { get; set; }
}
public class MyTypeClass2
{
public static void Main(string[] args)
{
try
{
// Get Type object of MyClass2.
Type myType = typeof(MyClass2);
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
PropertyInfo myPropInfo = myType.GetProperty(
"MyProperty",
BindingFlags.Public | BindingFlags.Instance
);
// Display Name property to console.
Console.WriteLine("{0} is a property of MyClass2.", myPropInfo.Name);
}
catch (NullReferenceException e)
{
Console.WriteLine("MyProperty does not exist in MyClass2." + e.Message);
}
}
}
open System
open System.Reflection
type MyClass() =
let mutable myProperty = 0
// Declare MyProperty.
member _.MyProperty
with get () =
myProperty
and set (value) =
myProperty <- value
try
// Get Type object of MyClass.
let myType = typeof<MyClass>
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
let myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public ||| BindingFlags.Instance)
// Display Name property to console.
printfn $"{myPropInfo.Name} is a property of MyClass."
with :? NullReferenceException as e ->
printfn $"MyProperty does not exist in MyClass.{e.Message}"
Imports System.Reflection
Module Module1
Public Class MyClass1
Private myProperty1 As Integer
Public Property MyProperty() As Integer
Get
Return myProperty1
End Get
Set(ByVal Value As Integer)
myProperty1 = Value
End Set
End Property
Public Shared Sub Run()
Try
' Get a Type object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get a PropertyInfo object by passing property name and specifying BindingFlags.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
' Display the Name property.
Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
End Module 'Module1
另请参阅
- PropertyInfo
- String
- BindingFlags
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String)
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
搜索具有指定名称的公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)]
public System.Reflection.PropertyInfo? GetProperty(string name);
public System.Reflection.PropertyInfo GetProperty(string name);
public System.Reflection.PropertyInfo? GetProperty(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties)>]
member this.GetProperty : string -> System.Reflection.PropertyInfo
member this.GetProperty : string -> System.Reflection.PropertyInfo
abstract member GetProperty : string -> System.Reflection.PropertyInfo
override this.GetProperty : string -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String) As PropertyInfo
参数
- name
- String
包含要获取的公共属性的名称的字符串。
返回
一个对象,表示具有指定名称的公共属性(如果找到);否则,为 null.
实现
- 属性
例外
找到具有指定名称的多个属性。
name 是 null。
示例
以下示例检索 Type 用户定义的类的对象,检索该类的属性,并显示属性名称。
using System;
using System.Reflection;
class MyClass1
{
// Declare MyProperty.
public int MyProperty { get; set; }
}
public class MyTypeClass1
{
public static void Main(string[] args)
{
try
{
// Get the Type object corresponding to MyClass1.
Type myType = typeof(MyClass1);
// Get the PropertyInfo object by passing the property name.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
// Display the property name.
Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.Name);
}
catch (NullReferenceException e)
{
Console.WriteLine("The property does not exist in MyClass1." + e.Message);
}
}
}
open System
type MyClass() =
let mutable myProperty = 0
// Declare MyProperty.
member _.MyProperty
with get () =
myProperty
and set (value) =
myProperty <- value
try
// Get the Type object corresponding to MyClass.
let myType = typeof<MyClass>
// Get the PropertyInfo object by passing the property name.
let myPropInfo = myType.GetProperty "MyProperty"
// Display the property name.
printfn $"The {myPropInfo.Name} property exists in MyClass."
with :? NullReferenceException as e ->
printfn $"The property does not exist in MyClass.{e.Message}"
Imports System.Reflection
Class MyClass1
Private myProperty1 As Integer
' Declare MyProperty.
Public Property MyProperty() As Integer
Get
Return MyProperty1
End Get
Set(ByVal Value As Integer)
MyProperty1 = Value
End Set
End Property
End Class
Public Class MyTypeClass
Public Shared Sub Run(ByVal args() As String)
Try
' Get Type Object corresponding to MyClass.
Dim myType As Type = GetType(MyClass1)
' Get PropertyInfo object by passing property name.
Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
' Display Name property to console.
Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
Catch e As NullReferenceException
Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
End Try
End Sub
End Class
注解
以下指南适用于所有重载:
- 如果属性至少有一个访问器是公共的,则将该属性视为可供反射的公共属性。 否则,该属性被视为私有属性,并且必须使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static(在 Visual Basic 中,将值组合在一
Or起)来获取该属性。 - 如果当前 Type 表示已构造的泛型类型,则此方法返回的 PropertyInfo 中,类型参数已被相应的类型实参替换。
- 如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的属性。
GetProperty(String) 方法
搜索 name 区分大小写。 搜索包括公共静态和公共实例属性。
发生 AmbiguousMatchException 的情况包括:
- 类型包含两个具有相同名称但不同数量的参数的索引属性。 若要解决歧义,请使用指定参数类型的 GetProperty 方法的重载。
- 派生类型通过使用
new修饰符(在 Visual Basic 中为Shadows),声明一个属性,该属性会隐藏具有相同名称的继承属性。 若要解决歧义,请使用 GetProperty(String, BindingFlags) 方法重载并添加 BindingFlags.DeclaredOnly 标志以将搜索限制为未继承的成员。
GetProperty(String, BindingFlags) 方法
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些属性:
- 您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。 - 指定
BindingFlags.Public在搜索中包含公共属性。 - 指定
BindingFlags.NonPublic在搜索中包含非公共属性(即专用属性、内部属性和受保护属性)。 - 指定
BindingFlags.FlattenHierarchy为包含public层次结构中的静态成员和protected静态成员;private不包括继承类中的静态成员。
以下 BindingFlags 修饰符标志可用于更改搜索的工作原理:
-
BindingFlags.IgnoreCase用于忽略name的大小写。 -
BindingFlags.DeclaredOnly仅搜索在Type中声明的属性,而不是继承的属性。
发生 AmbiguousMatchException 的情况包括:
- 类型包含两个具有相同名称但不同数量的参数的索引属性。 若要解决歧义,请使用指定参数类型的 GetProperty 方法的重载。
- 派生类型声明一个属性,该属性使用
new修饰符(Shadows在 Visual Basic 中)隐藏具有相同名称的继承属性。 若要解决歧义,请包括 BindingFlags.DeclaredOnly 将搜索限制为未继承的成员。
GetProperty(System.String、System.Reflection.BindingFlags、System.Reflection.Binder、System.Type、System.Type[]、System.Reflection.ParameterModifier[]) 方法
尽管默认联编程序不处理 ParameterModifier ( modifiers 参数),但可以使用抽象 System.Reflection.Binder 类编写一个处理自定义 modifiers联编程序。
ParameterModifier 仅在通过 COM 互作调用时使用,并且仅处理通过引用传递的参数。
下表显示了通过 Get 方法在反射类型时返回的基类成员。
| 成员类型 | Static | 非静态 |
|---|---|---|
| 构造函数 | 否 | 否 |
| 领域 | 否 | 是的。 字段始终是按名称和签名隐藏的。 |
| Event | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为通过名称和签名进行隐藏。2 |
| 方法 | 否 | 是的。 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。 |
| 嵌套类型 | 否 | 否 |
| 财产 | 不適用 | 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为通过名称和签名进行隐藏。2 |
Notes:
- 按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。
- 对于反射,属性和事件通过名称和签名进行隐藏。 如果基类中同时具有 get 和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且无法访问基类上的 setter。
- 自定义属性不是常见类型系统的一部分。
以下 BindingFlags 筛选器标志可用于定义要在搜索中包含哪些属性:
- 您必须指定
BindingFlags.Instance或BindingFlags.Static才能获得返回结果。 - 指定
BindingFlags.Public在搜索中包含公共属性。 - 指定
BindingFlags.NonPublic在搜索中包含非公共属性(即专用属性、内部属性和受保护属性)。 - 指定
BindingFlags.FlattenHierarchy为包含public层次结构中的静态成员和protected静态成员;private不包括继承类中的静态成员。
以下 BindingFlags 修饰符标志可用于更改搜索的工作原理:
-
BindingFlags.IgnoreCase用于忽略name的大小写。 -
BindingFlags.DeclaredOnly仅搜索在Type中声明的属性,而不是继承的属性。
索引器和默认属性
Visual Basic、C# 和 C++的语法简化了访问索引属性的语法,并允许一个索引属性作为其类型的默认值。 例如,如果变量 myList 引用了一个 ArrayList,则语法 myList[3] (myList(3) 在 Visual Basic 中)检索索引为 3 的元素。 你可以重载此属性。
在 C# 中,此功能称为索引器,不能按名称引用。 默认情况下,C# 索引器在元数据中显示为名为 Item 的索引属性。 但是,类库开发人员可以使用 IndexerNameAttribute 属性更改元数据中的索引器的名称。 例如,该 String 类具有一个名为 Chars[] 的索引器。 使用 C# 以外的语言创建的索引属性可以具有与 Item 不同的名称。
若要确定类型是否具有默认属性,请使用 GetCustomAttributes(Type, Boolean) 该方法测试 DefaultMemberAttribute 属性。 如果该类型具有 DefaultMemberAttribute,则 MemberName 属性返回默认属性的名称。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)