Type.GetProperty 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取当前 Type 的特定属性。
重载
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) |
使用指定的绑定约束,搜索参数与指定的自变量类型及修饰符匹配的指定属性。 |
GetProperty(String, Type, Type[], ParameterModifier[]) |
搜索其参数与指定自变量类型及修饰符匹配的指定公共属性。 |
GetProperty(String, Type[]) |
搜索其参数与指定自变量类型匹配的指定公共属性。 |
GetProperty(String, Type, Type[]) |
搜索其参数与指定自变量类型匹配的指定公共属性。 |
GetProperty(String, BindingFlags) |
使用指定的绑定约束搜索指定属性。 |
GetProperty(String) |
搜索具有指定名称的公共属性。 |
GetProperty(String, Type) |
搜索具有指定名称和返回类型的公共属性。 |
GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- 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);
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);
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
一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。
或
要使用 Nothing
的空引用(在 Visual Basic 中为 DefaultBinder)。
- returnType
- Type
属性的返回类型。
- types
- Type[]
一个 Type 对象数组,表示要获取的索引属性的参数的数目、顺序和类型。
或
获取未被索引的属性的 Type 类型的空数组(即 Type[] types = new Type[0])。
- modifiers
- ParameterModifier[]
ParameterModifier 对象的数组,表示与 types
数组中的相应元素关联的特性。 默认的联编程序不处理此参数。
返回
表示符合指定需求的属性的对象(如果找到的话);否则为 null
。
实现
例外
找到多个具有指定名称的属性且属性与指定绑定约束匹配。
types
的元素为 null
。
注解
有关此 API 的详细信息,请参阅 Type.GetProperty 的补充 API 备注。
另请参阅
- 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
搜索其参数与指定自变量类型及修饰符匹配的指定公共属性。
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);
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);
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 类型的空数组(即 Type[] types = new Type[0])。
- modifiers
- ParameterModifier[]
ParameterModifier 对象的数组,表示与 types
数组中的相应元素关联的特性。 默认的联编程序不处理此参数。
返回
表示符合指定要求的公共属性的对象(如果找到的话);否则为 null
。
实现
例外
找到多个具有指定名称且与指定参数类型和修饰符匹配的属性。
types
的元素为 null
。
示例
下面的示例获取对应于 Type
MyPropertyClass
的 对象,并使用传递给 GetProperty
方法的参数检索此类的索引属性。
using namespace System;
using namespace System::Reflection;
public ref class MyPropertyClass
{
private:
array<int, 2>^ myPropertyArray;
public:
property int Item [int, int]
{
// Declare an indexer.
int get( int i, int j )
{
return myPropertyArray[ i,j ];
}
void set( int i, int j, int value )
{
myPropertyArray[ i,j ] = value;
}
}
};
int main()
{
try
{
Type^ myType = MyPropertyClass::typeid;
array<Type^>^myTypeArray = gcnew array<Type^>(2);
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray->SetValue( int::typeid, 0 );
myTypeArray->SetValue( int::typeid, 1 );
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
PropertyInfo^ myPropertyInfo = myType->GetProperty( "Item", int::typeid, myTypeArray, nullptr );
Console::WriteLine( "{0}.{1} has a property type of {2}", myType->FullName, myPropertyInfo->Name, myPropertyInfo->PropertyType );
}
catch ( Exception^ ex )
{
Console::WriteLine( "An exception occurred {0}", ex->Message );
}
}
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 MyTypeClass
Public Shared Sub Main()
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
注解
如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 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
搜索其参数与指定自变量类型匹配的指定公共属性。
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);
public System.Reflection.PropertyInfo? GetProperty (string name, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type[] types);
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 类型的空数组(即 Type[] types = new Type[0])。
返回
表示其参数与指定参数类型匹配的公共属性的对象(如果找到的话);否则为 null
。
实现
例外
找到多个具有指定名称且与指定自变量类型匹配的属性。
types
是多维的。
types
的元素为 null
。
示例
以下示例检索 Type
用户定义类的 对象,检索该类的 属性,并显示传递给 GetProperty
的参数所指定的属性名称和类型。
using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
array<int, 2>^myArray;
public:
property int Item [int, int]
{
// Declare an indexer.
int get( int i, int j )
{
return myArray[ i,j ];
}
void set( int i, int j, int value )
{
myArray[ i,j ] = value;
}
}
};
int main()
{
try
{
// Get the Type object.
Type^ myType = MyClass1::typeid;
array<Type^>^myTypeArr = gcnew array<Type^>(2);
// Create an instance of a Type array.
myTypeArr->SetValue( int::typeid, 0 );
myTypeArr->SetValue( int::typeid, 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 MyClass1.", myPropInfo );
}
catch ( NullReferenceException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Source : {0}", e->Source );
Console::WriteLine( "Message : {0}", e->Message );
}
}
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 Module1
Class MyClass1
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 MyTypeClass
Public Shared Sub Main()
Try
' Get the Type Object.
Dim myType As Type = GetType(MyClass1)
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 MyClass1.", 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 'Module1
注解
如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or
) 组合这些值来获取它。
搜索 name
区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, Type, Type[])
- 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);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types);
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 类型的空数组(即 Type[] types = new Type[0])。
返回
表示其参数与指定参数类型匹配的公共属性的对象(如果找到的话);否则为 null
。
实现
例外
找到多个具有指定名称且与指定自变量类型匹配的属性。
types
是多维的。
types
的元素为 null
。
注解
如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 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
使用指定的绑定约束搜索指定属性。
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::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);
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 namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
int myProperty;
public:
property int MyProperty
{
// Declare MyProperty.
int get()
{
return myProperty;
}
void set( int value )
{
myProperty = value;
}
}
};
int main()
{
try
{
// Get Type object of MyClass.
Type^ myType = MyClass::typeid;
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
// Display Name property to console.
Console::WriteLine( "{0} is a property of MyClass.", myPropInfo->Name );
}
catch ( NullReferenceException^ e )
{
Console::WriteLine( "MyProperty does not exist in MyClass. {0}", e->Message );
}
}
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
' Declare MyProperty.
Public Property MyProperty() As Integer
Get
Return myProperty1
End Get
Set(ByVal Value As Integer)
myProperty1 = Value
End Set
End Property
Public Shared Sub Main()
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
搜索具有指定名称的公共属性。
public:
System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public:
virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public System.Reflection.PropertyInfo? GetProperty (string name);
public System.Reflection.PropertyInfo GetProperty (string name);
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 namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
int myProperty;
public:
property int MyProperty
{
// Declare MyProperty.
int get()
{
return myProperty;
}
void set( int value )
{
myProperty = value;
}
}
};
int main()
{
try
{
// Get the Type object corresponding to MyClass.
Type^ myType = MyClass::typeid;
// 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 MyClass.", myPropInfo->Name );
}
catch ( NullReferenceException^ e )
{
Console::WriteLine( "The property does not exist in MyClass. {0}", e->Message );
}
}
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 Main(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
注解
有关此 API 的详细信息,请参阅 Type.GetProperty 的补充 API 备注。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)
适用于
GetProperty(String, Type)
- 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);
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType);
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 namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
String^ myMessage;
public:
property String^ MyProperty1
{
String^ get()
{
return myMessage;
}
void set( String^ value )
{
myMessage = value;
}
}
};
int main()
{
try
{
Type^ myType = MyClass1::typeid;
// Get the PropertyInfo Object* representing MyProperty1.
PropertyInfo^ myStringProperties1 = myType->GetProperty( "MyProperty1", String::typeid );
Console::WriteLine( "The name of the first property of MyClass1 is {0}.", myStringProperties1->Name );
Console::WriteLine( "The type of the first property of MyClass1 is {0}.", myStringProperties1->PropertyType );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException : {0}", e->Message );
}
catch ( AmbiguousMatchException^ e )
{
Console::WriteLine( "AmbiguousMatchException : {0}", 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 MyClass1 is MyProperty1.
//The type of the first property of MyClass1 is System.String.
}
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 MyClass1
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 Main()
Try
Dim myType As Type = GetType(MyClass1)
' Get the PropertyInfo object representing MyProperty1.
Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))
Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name)
Console.WriteLine("The type of the first property of MyClass1 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 MyClass1 is MyProperty1.
'The type of the first property of MyClass1 is System.String.
End Sub
End Class
注解
如果属性至少有一个公共访问器,则它被视为公共的反射。 否则,该属性被视为私有属性,并且必须在 Visual Basic 中使用 BindingFlags.NonPublic | | BindingFlags.InstanceBindingFlags.Static (,使用 Or
) 组合这些值来获取它。
搜索 name
区分大小写。 搜索包括公共静态和公共实例属性。
如果当前 Type 表示构造的泛型类型,则此方法返回 PropertyInfo ,并将类型参数替换为相应的类型参数。
如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法将搜索类约束的属性。
另请参阅
- PropertyInfo
- String
- DefaultBinder
- GetPropertyImpl(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])
- GetProperties(BindingFlags)