英語で読む

次の方法で共有


Type.GetProperty メソッド

定義

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

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
C#
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);

パラメーター

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 です。

注釈

この API の詳細については、「 Type.GetProperty の補足 API 解説」を参照してください。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

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

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
C#
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);

パラメーター

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 取得します。

C#
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);
        }
    }
}

注釈

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

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

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

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

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

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type[])

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, Type[] types);
C#
public System.Reflection.PropertyInfo GetProperty (string name, Type[] types);

パラメーター

name
String

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

types
Type[]

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

または

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

戻り値

指定した引数型と一致するパラメーターが設定されているパブリック プロパティが存在する場合は、そのパブリック プロパティを表すオブジェクト。それ以外の場合は null

実装

例外

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

namenullです。

または

typesnullです。

types が多次元です。

types の要素は null です。

次の例では、ユーザー定義クラスのオブジェクトを取得 Type し、そのクラスのプロパティを取得し、 に渡される引数で指定されたプロパティのプロパティ名と型を GetProperty表示します。

C#

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);
        }
    }
}

注釈

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

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

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

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

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type, Type[])

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType, Type[] types);
C#
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types);

パラメーター

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 の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約のプロパティを検索します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, BindingFlags)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
C#
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr);

パラメーター

name
String

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

bindingAttr
BindingFlags

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

または

null を返す場合は Default

戻り値

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

実装

例外

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

namenullです。

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

C#

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);
        }
    }
}

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name);
C#
public System.Reflection.PropertyInfo GetProperty (string name);

パラメーター

name
String

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

戻り値

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

実装

例外

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

namenullです。

次の例では、ユーザー定義クラスのオブジェクトを取得 Type し、そのクラスのプロパティを取得し、プロパティ名を表示します。

C#
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);
        }
    }
}

注釈

この API の詳細については、「 Type.GetProperty の補足 API 解説」を参照してください。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetProperty(String, Type)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

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

C#
public System.Reflection.PropertyInfo? GetProperty (string name, Type? returnType);
C#
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType);

パラメーター

name
String

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

returnType
Type

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

戻り値

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

実装

例外

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

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

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

C#

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

注釈

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

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

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

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

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1