英語で読む

次の方法で共有


Attribute.IsDefined メソッド

定義

指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバー、またはメソッド パラメーターに適用されているかどうかを判断します。

オーバーロード

IsDefined(ParameterInfo, Type, Boolean)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーター、検索対象のカスタム属性の型、およびそのメソッド パラメーターの先祖を検索するかどうかを指定します。

IsDefined(Module, Type, Boolean)

カスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュール、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

IsDefined(MemberInfo, Type, Boolean)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

IsDefined(Assembly, Type, Boolean)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリ、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

IsDefined(MemberInfo, Type)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバーと検索対象のカスタム属性の型を指定します。

IsDefined(Module, Type)

指定した型のカスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュールと検索対象のカスタム属性の型を指定します。

IsDefined(Assembly, Type)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリと検索対象のカスタム属性の型を指定します。

IsDefined(ParameterInfo, Type)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーターと検索対象のカスタム属性の型を指定します。

IsDefined(ParameterInfo, Type, Boolean)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーター、検索対象のカスタム属性の型、およびそのメソッド パラメーターの先祖を検索するかどうかを指定します。

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

パラメーター

element
ParameterInfo

クラスのメンバーのパラメーターを記述する ParameterInfo クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

inherit
Boolean

true の場合は、element の先祖のカスタム属性も検索することを示します。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

element がメソッド、コンストラクター、型のいずれでもありません。

次のコード例は、パラメーターとして受ParameterInfoけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */

適用対象

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

IsDefined(Module, Type, Boolean)

カスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュール、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType, bool inherit);

パラメーター

element
Module

移植可能な実行可能 (PE) ファイルを記述する Module クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

inherit
Boolean

このパラメーターは無視され、このメソッドの動作には影響しません。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

次のコード例は、パラメーターとして受Moduleけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */

注釈

このメソッドはパラメーターを inherit 無視し、カスタム属性の element 先祖を検索しません。

適用対象

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

IsDefined(MemberInfo, Type, Boolean)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType, bool inherit);

パラメーター

element
MemberInfo

クラスのコンストラクター メンバー、イベント メンバー、フィールド メンバー、メソッド メンバー、型メンバー、またはプロパティ メンバーを記述する MemberInfo クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

inherit
Boolean

true の場合は、element の先祖のカスタム属性も検索することを示します。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

elementがコンストラクター、メソッド、プロパティ、イベント、型、またはフィールドではありません。

次のコード例は、パラメーターとして受MemberInfoけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */

注釈

注意

.NET Framework バージョン 2.0 以降、このメソッドは、型、メソッド、またはコンストラクターに新しいメタデータ形式で格納されているセキュリティ属性がある場合に戻りますtrue。 バージョン 2.0 以降でコンパイルされたアセンブリでは、新しい形式が使用されます。 以前のバージョンの.NET Frameworkでコンパイルされた動的アセンブリとアセンブリは、古い XML 形式を使用します。 宣言型セキュリティ属性の出力を参照してください。

適用対象

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

IsDefined(Assembly, Type, Boolean)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリ、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType, bool inherit);

パラメーター

element
Assembly

モジュールの再利用可能なコレクションを記述する Assembly クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

inherit
Boolean

このパラメーターは無視され、このメソッドの動作には影響しません。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

次のコード例は、パラメーターとして受け取る Assembly 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */

注釈

注意

.NET Framework バージョン 2.0 以降、アセンブリに新しいメタデータ形式で格納されているセキュリティ属性がある場合、このメソッドは返trueします。 バージョン 2.0 以降でコンパイルされたアセンブリでは、新しい形式が使用されます。 以前のバージョンの.NET Frameworkでコンパイルされた動的アセンブリとアセンブリは、古い XML 形式を使用します。 宣言型セキュリティ属性の出力を参照してください。

適用対象

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

IsDefined(MemberInfo, Type)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバーと検索対象のカスタム属性の型を指定します。

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType);

パラメーター

element
MemberInfo

クラスのコンストラクター メンバー、イベント メンバー、フィールド メンバー、メソッド メンバー、型メンバー、またはプロパティ メンバーを記述する MemberInfo クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

elementがコンストラクター、メソッド、プロパティ、イベント、型、またはフィールドではありません。

次のコード例は、パラメーターとして受MemberInfoけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */

注釈

の先祖 element は、カスタム属性を検索します。

注意

.NET Framework バージョン 2.0 以降、このメソッドは、型、メソッド、またはコンストラクターに新しいメタデータ形式で格納されているセキュリティ属性がある場合に戻りますtrue。 バージョン 2.0 以降でコンパイルされたアセンブリでは、新しい形式が使用されます。 以前のバージョンの.NET Frameworkでコンパイルされた動的アセンブリとアセンブリは、古い XML 形式を使用します。 宣言型セキュリティ属性の出力を参照してください。

適用対象

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

IsDefined(Module, Type)

指定した型のカスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュールと検索対象のカスタム属性の型を指定します。

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType);

パラメーター

element
Module

移植可能な実行可能 (PE) ファイルを記述する Module クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

次のコード例は、パラメーターとして受Moduleけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */

注釈

の先祖 element は、カスタム属性を検索しません。

適用対象

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

IsDefined(Assembly, Type)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリと検索対象のカスタム属性の型を指定します。

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType);

パラメーター

element
Assembly

モジュールの再利用可能なコレクションを記述する Assembly クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

次のコード例は、パラメーターとして受け取る Assembly 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */

注釈

注意

.NET Framework バージョン 2.0 以降、アセンブリに新しいメタデータ形式で格納されているセキュリティ属性がある場合、このメソッドは返trueします。 バージョン 2.0 以降でコンパイルされたアセンブリでは、新しい形式が使用されます。 以前のバージョンの.NET Frameworkでコンパイルされた動的アセンブリとアセンブリは、古い XML 形式を使用します。 宣言型セキュリティ属性の出力を参照してください。

適用対象

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

IsDefined(ParameterInfo, Type)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーターと検索対象のカスタム属性の型を指定します。

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType);

パラメーター

element
ParameterInfo

クラスのメンバーのパラメーターを記述する ParameterInfo クラスから派生したオブジェクト。

attributeType
Type

検索対象とするカスタム属性の型または基本型。

戻り値

Boolean

attributeType 型のカスタム属性が element に適用される場合は true。それ以外の場合は false

例外

element または attributeTypenull です。

attributeTypeAttribute から派生していません。

次のコード例は、パラメーターとして受ParameterInfoけ取る 、のIsDefined使用方法を示しています。

C#
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */

注釈

の先祖 element は、カスタム属性を検索します。

適用対象

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