英語で読む

次の方法で共有


ParameterInfo.IsDefined(Type, Boolean) メソッド

定義

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

C#
public virtual bool IsDefined(Type attributeType, bool inherit);

パラメーター

attributeType
Type

検索する Type オブジェクト。

inherit
Boolean

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

戻り値

このパラメーターに attributeType またはその派生型のインスタンスが 1 つ以上適用されている場合は true。それ以外の場合は false

実装

例外

attributeTypenullです。

attributeType が、共通言語ランライムで提供されている Type オブジェクトではありません。

次の例では、 MyAttributeMyDerivedAttributeの 2 つのカスタム属性を定義します。 MyDerivedAttribute は、MyAttribute から派生しています。 次に、これらの属性をサンプル クラスのメソッドのパラメーターに適用します。

この例を実行すると、 メソッドを IsDefined 使用して、サンプル クラス内のすべてのメソッドのすべてのパラメーターをテストします。 次に、 または MyDerivedAttributeを持つMyAttributeパラメーターが表示されます。

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

// Derive another custom attribute from MyAttribute
[AttributeUsage(AttributeTargets.Parameter)]
public class MyDerivedAttribute : MyAttribute
{
    public MyDerivedAttribute(string name) : base(name) {}
}

// Define a class with a method that has three parameters. Apply
// MyAttribute to one parameter, MyDerivedAttribute to another, and
// no attributes to the third.
public class MyClass1
{
    public void MyMethod(
        [MyAttribute("This is an example parameter attribute")]
        int i,
        [MyDerivedAttribute("This is another parameter attribute")]
        int j,
        int k )
    {
        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();

        // For each method of the class 'MyClass1', display all the parameters
        // to which MyAttribute or its derived types have been applied.
        foreach (MethodInfo mi in myMethods)
        {
            // Get the parameters for the method.
            ParameterInfo[] myParameters = mi.GetParameters();
            if (myParameters.Length > 0)
            {
                Console.WriteLine("\nThe following parameters of {0} have MyAttribute or a derived type: ", mi);
                foreach (ParameterInfo pi in myParameters)
                {
                    if (pi.IsDefined(typeof(MyAttribute), false))
                    {
                        Console.WriteLine("Parameter {0}, name = {1}, type = {2}",
                            pi.Position, pi.Name, pi.ParameterType);
                    }
                }
            }
        }
    }
}

/* This code example produces the following output:

The following parameters of Void MyMethod(Int32, Int32, Int32) have MyAttribute or a derived type:
Parameter 0, name = i, type = System.Int32
Parameter 1, name = j, type = System.Int32

The following parameters of Boolean Equals(System.Object) have MyAttribute or a derived type:
 */

注釈

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

適用対象

製品 バージョン
.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