指定した名前のパブリック プロパティを検索します。
Overloads Public Function GetProperty( _
ByVal name As String _) As PropertyInfo
[C#]
public PropertyInfo GetProperty(stringname);
[C++]
public: PropertyInfo* GetProperty(String* name);
[JScript]
public function GetProperty(
name : String) : PropertyInfo;
パラメータ
- name
取得するパブリック プロパティの名前を格納している String 。
戻り値
指定した名前のパブリック プロパティが存在する場合は、そのパブリック プロパティを表す PropertyInfo オブジェクト。それ以外の場合は null 参照 (Visual Basic では Nothing) 。
例外
例外の種類 | 条件 |
---|---|
AmbiguousMatchException | 指定した名前のプロパティが 2 つ以上存在します。 |
ArgumentNullException | name が null 参照 (Visual Basic では Nothing) です。 |
解説
name の検索では大文字と小文字が区別されます。
要求された型がパブリックではなく、呼び出し元に現在のアセンブリ外の非パブリック オブジェクトをリフレクションするための ReflectionPermission がない場合、このメソッドは null 参照 (Visual Basic では Nothing) を返します。
使用例
[Visual Basic, C#, C++] ユーザー定義クラスの Type オブジェクトと、そのクラスのプロパティを取得して、プロパティ名を表示する例を次に示します。
Imports System
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 'MyClass1
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 propety 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 'Main
End Class 'MyTypeClass
[C#]
using System;
using System.Reflection;
class MyClass
{
private int myProperty;
// Declare MyProperty.
public int MyProperty
{
get
{
return myProperty;
}
set
{
myProperty=value;
}
}
}
public class MyTypeClass
{
public static void Main(string[] args)
{
try
{
// Get the Type object corresponding to MyClass.
Type myType=typeof(MyClass);
// 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." + e.Message);
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
__gc class MyClass {
private:
int myProperty;
// Declare MyProperty.
public:
__property int get_MyProperty() {
return myProperty;
}
__property void set_MyProperty(int value) {
myProperty=value;
}
};
int main() {
try {
// Get the Type object corresponding to MyClass.
Type* myType=__typeof(MyClass);
// Get the PropertyInfo object by passing the property name.
PropertyInfo* myPropInfo = myType->GetProperty(S"MyProperty");
// Display the property name.
Console::WriteLine(S"The {0} property exists in MyClass.", myPropInfo->Name);
} catch (NullReferenceException* e) {
Console::WriteLine(S"The property does not exist in MyClass. {0}", e->Message);
}
}
[Visual Basic, C#, C++] 内部的には、このプロパティはメタデータで "Item" という名前で参照されます。したがって、リフレクションを使用して PropertyInfo を取得する場合は、正しい PropertyInfo が返されるように、この内部名を指定する必要があります。
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Type クラス | Type メンバ | System 名前空間 | Type.GetProperty オーバーロードの一覧 | PropertyInfo | String | DefaultBinder | ReflectionPermission | GetPropertyImpl | GetProperties