Type.GetProperty メソッド

定義

現在の 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[])

指定したパブリック プロパティのうち、指定した引数型と一致するパラメーターが設定されているものを検索します。

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

実装

例外

指定した名前を持ち引数の型が一致する複数のプロパティが存在します。

namenullです。

または

typesnullです。

types が多次元です。

types の要素は null です。

注釈

プロパティは、少なくとも 1 つのアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、(Visual Basic では、 を使用して値を結合する) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

name 検索では、大文字と小文字が区別されます。 検索には、パブリック静的インスタンスプロパティとパブリック インスタンス プロパティが含まれます。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは、 を適切な型引数に置き換えられた型パラメーターで返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 には、インデックス付きプロパティにアクセスするための構文が簡略化されており、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に がある場合、 プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

指定したバインディング制約を使用して、指定した引数の型および修飾子と一致するパラメーターが設定された指定のプロパティを検索します。

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

取得するプロパティの名前を格納している文字列。

bindingAttr
BindingFlags

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または

null を返す場合は Default

binder
Binder

一連のプロパティを定義し、バインディングを有効にするオブジェクト。バインディングには、オーバーロードされたメソッドの選択、引数の型の強制変換、リフレクションによるメンバーの呼び出しなどが含まれます。

または

Nothing を使用する場合は、null 参照 (Visual Basic の場合は DefaultBinder)。

returnType
Type

プロパティの戻り値の型。

types
Type[]

取得するインデックス付きプロパティに対するパラメーターの数値、順序、および型を表す Type オブジェクトの配列。

または

インデックス付けされていないプロパティを取得するための、Type 型の空の配列 (Type[] types = new Type[0])。

modifiers
ParameterModifier[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件と一致するプロパティが存在する場合は、そのプロパティを表すオブジェクト。それ以外の場合は null

実装

例外

指定された名前を持ち、指定されたバインディング制約に一致する 1 つ以上のプロパティが存在します。

namenullです。

または

typesnullです。

types が多次元です。

または

modifiers が多次元です。

または

typesmodifiers の長さが同じではありません。

types の要素は null です。

注釈

プロパティは、少なくとも 1 つのアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、(Visual Basic では、 を使用して値を結合する) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

既定のバインダーでは (パラメーター) はmodifiers処理ParameterModifierされませんが、抽象System.Reflection.Binderクラスを使用して、 を処理modifiersするカスタム バインダーを記述できます。 ParameterModifier は COM 相互運用機能を介して を呼び出すときにのみ使用され、参照渡しされるパラメーターのみが処理されます。

次の表は、型に反映するときに、 メソッドによって Get 返される基底クラスのメンバーを示しています。

メンバーの型 静的 非静的
コンストラクター いいえ いいえ
フィールド いいえ はい。 フィールドは常に名前と署名で非表示になります。
Event 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドと同じであるということです。 リフレクションでは、プロパティが名前と署名による非表示として扱われます。 以下の注 2 を参照してください。
メソッド いいえ はい。 メソッド (仮想と非仮想の両方) には、名前による非表示または名前と署名による非表示を指定できます。
入れ子になった型 いいえ いいえ
プロパティ 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドと同じであるということです。 リフレクションでは、プロパティが名前と署名による非表示として扱われます。 以下の注 2 を参照してください。
  1. 名前と署名による非表示では、カスタム修飾子、戻り値の型、パラメーターの型、sentinel、アンマネージド呼び出し規則など、署名のすべての部分が考慮されます。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名による非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーのみが含まれている場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

BindingFlags のフィルター フラグを使用して、検索に含めるプロパティを定義できます。

  • 戻り値を取得するには、 または BindingFlags.StaticBindingFlags.Instance指定する必要があります。

  • 検索にパブリック プロパティを含めるには、 を指定 BindingFlags.Public します。

  • 非パブリック プロパティ (プライベート、内部、保護されたプロパティ) を検索に含める場合は、 を指定 BindingFlags.NonPublic します。

  • 階層に含める静的メンバーとprotected静的メンバーを指定BindingFlags.FlattenHierarchypublicしますprivate。継承されたクラスの静的メンバーは含まれません。

検索の動作方法を変更するには、次 BindingFlags の修飾子フラグを使用できます。

  • BindingFlags.IgnoreCase の大文字と小文字を無視する場合 nameは 。

  • BindingFlags.DeclaredOnly で宣言されたプロパティのみを検索する場合は Type。単に継承されたプロパティは検索されません。

詳細については、「System.Reflection.BindingFlags」を参照してください。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは、 を適切な型引数に置き換えられた型パラメーターで返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 には、インデックス付きプロパティにアクセスするための構文が簡略化されており、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に がある場合、 プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String, Type, Type[], ParameterModifier[])

指定したパブリック プロパティのうち、指定した引数の型および修飾子と一致するパラメーターが設定されているものを検索します。

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[]

types 配列内の対応する要素に関連付けられている属性を表す ParameterModifier オブジェクトの配列。 既定のバインダーでは、このパラメーターは処理されません。

戻り値

指定した要件と一致するパブリック プロパティが存在する場合は、そのパブリック プロパティを表すオブジェクト。それ以外の場合は null

実装

例外

指定された名前を持ち、指定された引数の型および修飾子に一致する 2 つ以上のプロパティが存在します。

namenullです。

または

typesnullです。

types が多次元です。

または

modifiers が多次元です。

または

typesmodifiers の長さが同じではありません。

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 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

注釈

プロパティは、少なくとも 1 つのアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、(Visual Basic では、 を使用して値を結合する) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

既定のバインダーでは (パラメーター) はmodifiers処理ParameterModifierされませんが、抽象System.Reflection.Binderクラスを使用して、 を処理modifiersするカスタム バインダーを記述できます。 ParameterModifier は COM 相互運用機能を介して を呼び出すときにのみ使用され、参照渡しされるパラメーターのみが処理されます。

name 検索では、大文字と小文字が区別されます。 検索には、パブリック静的インスタンスプロパティとパブリック インスタンス プロパティが含まれます。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは、 を適切な型引数に置き換えられた型パラメーターで返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 には、インデックス付きプロパティにアクセスするための構文が簡略化されており、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に がある場合、 プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String, Type[])

指定したパブリック プロパティのうち、指定した引数型と一致するパラメーターが設定されているものを検索します。

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

実装

例外

指定した名前を持ち引数の型が一致する複数のプロパティが存在します。

namenullです。

または

typesnullです。

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 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

注釈

プロパティは、少なくとも 1 つのアクセサーがパブリックである場合、リフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、(Visual Basic では、 を使用して値を結合する) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

name 検索では、大文字と小文字が区別されます。 検索には、パブリック静的インスタンスプロパティとパブリック インスタンス プロパティが含まれます。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは、 を適切な型引数に置き換えられた型パラメーターで返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 には、インデックス付きプロパティにアクセスするための構文が簡略化されており、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に がある場合、 プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String, Type)

指定した名前および戻り値の型を持つパブリック プロパティを検索します。

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

実装

例外

指定した名前のプロパティが複数個見つかりました。

namenull であるか、または returnTypenull です。

次の例では、1 つのプロパティを持つクラスを定義し、プロパティの名前と型を取得します。

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
{
    String myMessage = "Hello World.";
    public string MyProperty1
    {
        get
        {			
            return myMessage;
        }
        set
        {
            myMessage = value;
        }			
    }
}

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

注釈

パブリックなアクセサーが少なくとも 1 つ存在する場合、プロパティはリフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、 (Visual Basic では、 を使用して値を組み合わせる) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

name 検索では、大文字と小文字が区別されます。 検索には、パブリック静的インスタンス プロパティとパブリック インスタンス プロパティが含まれます。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは 型パラメーターを適切な型引数に置き換えて を返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 では、インデックス付きプロパティにアクセスするための構文が簡略化され、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は、 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に が含まれている場合、プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String, BindingFlags)

指定されたバインディング制約を使用して、指定されたプロパティを検索します。

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

取得するプロパティの名前を格納している文字列。

bindingAttr
BindingFlags

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または

null を返す場合は Default

戻り値

指定した要件と一致するプロパティが存在する場合は、そのプロパティを表すオブジェクト。それ以外の場合は null

実装

例外

指定された名前を持ち、指定されたバインディング制約に一致する 1 つ以上のプロパティが存在します。

namenullです。

次の例では、ユーザー定義クラスの型を取得し、そのクラスのプロパティを取得し、指定されたバインド制約に従ってプロパティ名を表示します。

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
{
    private int myProperty;
    // Declare MyProperty.
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}

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

注釈

パブリックなアクセサーが少なくとも 1 つ存在する場合、プロパティはリフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、 (Visual Basic では、 を使用して値を組み合わせる) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

BindingFlags のフィルター フラグを使用して、検索に含めるプロパティを定義できます。

  • 戻り値を取得するには、 または BindingFlags.Static のいずれかをBindingFlags.Instance指定する必要があります。

  • 検索にパブリック プロパティを含めるには、 を指定 BindingFlags.Public します。

  • 非パブリック プロパティ (つまり、プライベート、内部、および保護されたプロパティ) を検索に含めるには、 を指定 BindingFlags.NonPublic します。

  • を指定BindingFlags.FlattenHierarchyして、階層privateのメンバーとprotected静的メンバーを含publicめます。継承されたクラスの静的メンバーは含まれません。

検索のしくみを変更するには、次 BindingFlags の修飾子フラグを使用できます。

  • BindingFlags.IgnoreCase の大文字と小文字を無視する場合 nameは 。

  • BindingFlags.DeclaredOnly で宣言された Typeプロパティのみを検索する場合は 。単に継承されたプロパティは検索されません。

詳細については、「System.Reflection.BindingFlags」を参照してください。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは 型パラメーターを適切な型引数に置き換えて を返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

次のような AmbiguousMatchException 状況が発生します。

  • 型には、同じ名前と異なる数のパラメーターを持つ 2 つのインデックス付きプロパティが含まれています。 あいまいさを解決するには、パラメーター型を指定する メソッドの GetProperty オーバーロードを使用します。

  • 派生型は、同じ名前の継承されたプロパティを非表示にするプロパティを宣言します。このプロパティは、 修飾子 (Shadows Visual Basic では ) を使用しますnew。 あいまいさを解決するには、 を含 BindingFlags.DeclaredOnly めて、継承されていないメンバーに検索を制限します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 では、インデックス付きプロパティにアクセスするための構文が簡略化され、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は、 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に が含まれている場合、プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象

GetProperty(String)

指定した名前のパブリック プロパティを検索します。

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

実装

例外

指定した名前のプロパティが複数個見つかりました。

namenullです。

次の例では、 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
{
    private int myProperty;
    // Declare MyProperty.
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}

public class MyTypeClass2
{
    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

内部的には、このプロパティはメタデータ内で "Item" という名前で参照されます。リフレクションを使用して取得 PropertyInfo しようとすると、 プロパティを正しく返すために、この内部名を指定する PropertyInfo 必要があります。

注釈

name 検索では、大文字と小文字が区別されます。 検索には、パブリック静的インスタンス プロパティとパブリック インスタンス プロパティが含まれます。

パブリックなアクセサーが少なくとも 1 つ存在する場合、プロパティはリフレクションに対してパブリックと見なされます。 それ以外の場合、プロパティはプライベートと見なされ、 (Visual Basic では、 を使用して値を組み合わせる) を使用BindingFlags.NonPublicOr | BindingFlags.Instance | BindingFlags.Staticして取得する必要があります。

現在 Type の が構築されたジェネリック型を表す場合、このメソッドは 型パラメーターを適切な型引数に置き換えて を返 PropertyInfo します。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

次のような AmbiguousMatchException 状況が発生します。

  • 型には、同じ名前と異なる数のパラメーターを持つ 2 つのインデックス付きプロパティが含まれています。 あいまいさを解決するには、パラメーター型を指定する メソッドの GetProperty オーバーロードを使用します。

  • 派生型は、同じ名前の継承されたプロパティを非表示にするプロパティを、Visual Basic の 修飾子 (Shadows) を使用newして宣言します。 あいまいさを解決するには、 メソッドのオーバーロードを GetProperty(String, BindingFlags) 使用し、 フラグを BindingFlags.DeclaredOnly 追加して、継承されていないメンバーに検索を制限します。

インデクサーと既定のプロパティ

Visual Basic 2005、Visual C# 2005、および Visual C++ 2005 では、インデックス付きプロパティにアクセスするための構文が簡略化され、1 つのインデックス付きプロパティがその型の既定値になります。 たとえば、変数 myList が を参照している ArrayList場合、構文 myList[3] (myList(3) Visual Basic の場合) は、インデックスが 3 の要素を取得します。 プロパティをオーバーロードできます。

C# では、この機能はインデクサーと呼ばれ、名前で参照することはできません。 既定では、C# インデクサーは"Item" という名前のインデックス付きプロパティとしてメタデータに表示されます。 ただし、クラス ライブラリ開発者は、 属性を IndexerNameAttribute 使用して、メタデータ内のインデクサーの名前を変更できます。 たとえば、 クラスには String という名前 Chars[]のインデクサーがあります。 C# 以外の言語を使用して作成されたインデックス付きプロパティには、Item 以外の名前を付けることもできます。

型に既定のプロパティがあるかどうかを判断するには、 メソッドを GetCustomAttributes(Type, Boolean) 使用して 属性を DefaultMemberAttribute テストします。 型に が含まれている場合、プロパティはDefaultMemberAttributeMemberName既定のプロパティの名前を返します。

こちらもご覧ください

適用対象