英語で読む

次の方法で共有


ParameterInfo.GetCustomAttributes メソッド

定義

このパラメーターに適用されているカスタム属性を取得します。

オーバーロード

GetCustomAttributes(Type, Boolean)

このパラメーターに適用されている、指定された型またはその派生型のカスタム属性を取得します。

GetCustomAttributes(Boolean)

このパラメーターに定義されているすべてのカスタム属性を取得します。

GetCustomAttributes(Type, Boolean)

ソース:
ParameterInfo.cs
ソース:
ParameterInfo.cs
ソース:
ParameterInfo.cs

このパラメーターに適用されている、指定された型またはその派生型のカスタム属性を取得します。

public virtual object[] GetCustomAttributes(Type attributeType, bool inherit);

パラメーター

attributeType
Type

型により識別されるカスタム属性。

inherit
Boolean

この型のオブジェクトでは、この引数は無視されます。

戻り値

Object[]

指定された型またはその派生型のカスタム属性が格納されている配列。

実装

例外

型は、基になるランタイム システムで提供されている型でなければなりません。

attributeTypenullです。

カスタム属性の型を読み込むことができませんでした。

注釈

このメソッドは、 パラメーターを inherit 無視します。 継承チェーンでパラメーターの属性を検索するには、 メソッドの適切なオーバーロードを Attribute.GetCustomAttributes 使用します。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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

GetCustomAttributes(Boolean)

ソース:
ParameterInfo.cs
ソース:
ParameterInfo.cs
ソース:
ParameterInfo.cs

このパラメーターに定義されているすべてのカスタム属性を取得します。

public virtual object[] GetCustomAttributes(bool inherit);

パラメーター

inherit
Boolean

この型のオブジェクトでは、この引数は無視されます。

戻り値

Object[]

このパラメーターに適用されているすべてのカスタム属性が格納されている配列。

実装

例外

カスタム属性の型を読み込むことができませんでした。

次の例は、メソッドのパラメーターに適用されているカスタム属性を実行時に取得する方法を示しています。 この例では、 という名前 MyAttribute のカスタム属性を定義し、パラメーターに適用できます。 次に、 という名前のクラスを という名前MyClassMyMethodのメソッドで定義し、 メソッドのパラメーターに適用しますMyAttribute

この例を実行すると、 メソッドを GetCustomAttributes(Boolean) 使用して、 内のすべてのメソッド MyClassのすべてのパラメーターに適用されているカスタム属性を取得し、コンソールに表示します。

using System;
using System.Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
    private string myName;
    public MyAttribute(string name)
    {
        myName = name;
    }
    public string Name
    {
        get
        {
            return myName;
        }
    }
}

// Define a class which has a custom attribute associated with one of the
// parameters of a method.
public class MyClass1
{
    public void MyMethod(
        [MyAttribute("This is an example parameter attribute")]
        int i)
    {
        return;
    }
}

public class MemberInfo_GetCustomAttributes
{
    public static void Main()
    {
        // Get the type of the class 'MyClass1'.
        Type myType = typeof(MyClass1);
        // Get the members associated with the class 'MyClass1'.
        MethodInfo[] myMethods = myType.GetMethods();

        // Display the attributes for each of the parameters of each method of the class 'MyClass1'.
        for(int i = 0; i < myMethods.Length; i++)
        {
            // Get the parameters for the method.
            ParameterInfo[] myParameters = myMethods[i].GetParameters();

            if (myParameters.Length > 0)
            {
                Console.WriteLine("\nThe parameters for the method {0} that have custom attributes are :", myMethods[i]);
                for(int j = 0; j < myParameters.Length; j++)
                {
                    // Get the attributes of type 'MyAttribute' for each parameter.
                    Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);

                    if (myAttributes.Length > 0)
                    {
                        Console.WriteLine("Parameter {0}, name = {1}, type = {2} has attributes: ",
                            myParameters[j].Position, myParameters[j].Name, myParameters[j].ParameterType);
                        for(int k = 0; k < myAttributes.Length; k++)
                        {
                            Console.WriteLine("\t{0}", myAttributes[k]);
                        }
                    }
                }
            }
        }
    }
}
/* This code example produces the following output:

The parameters for the method Void MyMethod(Int32) that have custom attributes are :
Parameter 0, name = i, type = System.Int32 has attributes:
        MyAttribute

The parameters for the method Boolean Equals(System.Object) that have custom attributes are :
 */

注釈

このメソッドは、 パラメーターを inherit 無視します。 継承チェーンでパラメーターの属性を検索するには、 メソッドの適切なオーバーロードを Attribute.GetCustomAttributes 使用します。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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