次の方法で共有


Attribute.GetCustomAttribute メソッド (ParameterInfo, Type)

指定したクラス メンバ パラメータに適用されている指定の型のカスタム属性、または基本クラスから継承された指定の型のカスタム属性を取得します。

Overloads Public Shared Function GetCustomAttribute( _
   ByVal element As ParameterInfo, _   ByVal attributeType As Type _) As Attribute
[C#]
public static Attribute GetCustomAttribute(ParameterInfoelement,TypeattributeType);
[C++]
public: static Attribute* GetCustomAttribute(ParameterInfo* element,Type* attributeType);
[JScript]
public static function GetCustomAttribute(
   element : ParameterInfo,attributeType : Type) : Attribute;

パラメータ

  • element
    クラスのメンバのパラメータを記述する ParameterInfo クラスから派生したオブジェクト。
  • attributeType
    カスタム属性を適用する対象の Type オブジェクト。

戻り値

element に適用される attributeType 型のカスタム属性が存在しない場合は null 参照 (Visual Basic では Nothing) 。

または

element に適用される attributeType 型の単一のカスタム属性への Attribute 参照。

例外

例外の種類 条件
ArgumentNullException element または attributeType が null 参照 (Visual Basic では Nothing) です。
ArgumentException attributeTypeAttribute から派生していません。
AmbiguousMatchException 要求された属性が複数見つかりました。

使用例

[Visual Basic, C#, C++] パラメータのカスタム属性クラスを定義し、そのカスタム属性を派生クラスおよび派生クラスの基本クラスのメソッドに適用するコード例を次に示します。この例では、 GetCustomAttribute メソッドを使用して属性を返します。

 
' Example for the Attribute.GetCustomAttribute( ParameterInfo, Type ) method.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Define a custom parameter attribute that takes a single message argument.
    <AttributeUsage(AttributeTargets.Parameter)>  _
    Public Class ArgumentUsageAttribute
        Inherits Attribute
           
        ' This is the attribute constructor.
        Public Sub New(UsageMsg As String)
            Me.usageMsg = UsageMsg
        End Sub ' New

        ' usageMsg is storage for the attribute message.
        Protected usageMsg As String
           
        ' This is the Message property for the attribute.
        Public Property Message() As String
            Get
                Return usageMsg
            End Get
            Set
                usageMsg = value
            End Set
        End Property
    End Class ' ArgumentUsageAttribute 

    Public Class BaseClass
       
        ' Assign an ArgumentUsage attribute to the strArray parameter.
        ' Assign a ParamArray attribute to strList using the ParamArray keyword.
        Public Overridable Sub TestMethod( _
            <ArgumentUsage("Must pass an array here.")> _
            strArray() As String, _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' BaseClass

    Public Class DerivedClass
        Inherits BaseClass
           
        ' Assign an ArgumentUsage attribute to the strList parameter.
        ' Assign a ParamArray attribute to strList using the ParamArray keyword.
        Public Overrides Sub TestMethod( _
            strArray() As String, _
            <ArgumentUsage("Can pass a parameter list or array here.")> _
            ParamArray strList() As String)
        End Sub ' TestMethod
    End Class ' DerivedClass

    Module CustomParamDemo
       
        Sub Main()
            Console.WriteLine( _
                "This example of Attribute.GetCustomAttribute" & _
                "( ParameterInfo, Type )" & vbCrLf & _
                "generates the following output.")
              
            ' Get the class type, and then get the MethodInfo object 
            ' for TestMethod to access its metadata.
            Dim clsType As Type = GetType(DerivedClass)
            Dim mInfo As MethodInfo = clsType.GetMethod("TestMethod")
              
            ' Iterate through the ParameterInfo array for the method parameters.
            Dim pInfoArray As ParameterInfo() = mInfo.GetParameters()
            If Not (pInfoArray Is Nothing) Then
                Dim paramInfo As ParameterInfo
                For Each paramInfo In  pInfoArray

                    ' See if the ParamArray attribute is defined.
                    Dim isDef As Boolean = _
                        Attribute.IsDefined(paramInfo, _
                            GetType(ParamArrayAttribute))

                    If isDef Then
                        Console.WriteLine( vbCrLf & _
                            "The ParamArray attribute is defined for " & _
                            vbCrLf & "parameter {0} of method {1}.", _
                            paramInfo.Name, mInfo.Name)
                    End If
                    
                    ' See if ParamUsageAttribute is defined.  
                    ' If so, display a message.
                    Dim usageAttr As ArgumentUsageAttribute = _
                        Attribute.GetCustomAttribute(paramInfo, _
                            GetType(ArgumentUsageAttribute))

                    If Not (usageAttr Is Nothing) Then
                        Console.WriteLine( vbCrLf & "The " & _
                            "ArgumentUsage attribute is defined for " & _
                            vbCrLf & "parameter {0} of method {1}.", _
                            paramInfo.Name, mInfo.Name)
                        Console.WriteLine( vbCrLf & _
                            "    The usage message for {0} is: " & _
                            vbCrLf & "    ""{1}"".", _
                            paramInfo.Name, usageAttr.Message)
                    End If
                Next paramInfo
            Else
                Console.WriteLine( _
                    "The parameters information could " & _
                    "not be retrieved for method {0}.", mInfo.Name)
            End If
        End Sub ' Main

    End Module ' DemoClass
End Namespace ' NDP_UE_VB

' This example of Attribute.GetCustomAttribute( ParameterInfo, Type )
' generates the following output.
' 
' The ArgumentUsage attribute is defined for
' parameter strArray of method TestMethod.
' 
'     The usage message for strArray is:
'     "Must pass an array here.".
' 
' The ParamArray attribute is defined for
' parameter strList of method TestMethod.
' 
' The ArgumentUsage attribute is defined for
' parameter strList of method TestMethod.
' 
'     The usage message for strList is:
'     "Can pass a parameter list or array here.".

[C#] 
// Example for the Attribute.GetCustomAttribute( ParameterInfo, Type ) method.
using System;
using System.Reflection;

namespace NDP_UE_CS 
{
    // Define a custom parameter attribute that takes a single message argument.
    [AttributeUsage( AttributeTargets.Parameter )]
    public class ArgumentUsageAttribute : Attribute
    {
        // This is the attribute constructor.
        public ArgumentUsageAttribute( string UsageMsg )
        {
            this.usageMsg = UsageMsg;
        }

        // usageMsg is storage for the attribute message.
        protected string usageMsg;

        // This is the Message property for the attribute.
        public string Message
        {
            get { return usageMsg; }
            set { usageMsg = value; }
        }
    }

    public class BaseClass 
    {
        // Assign an ArgumentUsage attribute to the strArray parameter.
        // Assign a ParamArray attribute to strList using the params keyword.
        public virtual void TestMethod(
            [ArgumentUsage("Must pass an array here.")]
            String[] strArray,
            params String[] strList)
        { }
    }

    public class DerivedClass : BaseClass
    {
        // Assign an ArgumentUsage attribute to the strList parameter.
        // Assign a ParamArray attribute to strList using the params keyword.
        public override void TestMethod(
            String[] strArray,
            [ArgumentUsage("Can pass a parameter list or array here.")]
            params String[] strList)
        { }
    }

    class CustomParamDemo 
    {
        static void Main( ) 
        {
            Console.WriteLine( 
                "This example of Attribute.GetCustomAttribute( Param" +
                "eterInfo, Type )\ngenerates the following output." );

            // Get the class type, and then get the MethodInfo object 
            // for TestMethod to access its metadata.
            Type clsType = typeof( DerivedClass );
            MethodInfo mInfo = clsType.GetMethod("TestMethod");

            // Iterate through the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfoArray = mInfo.GetParameters();
            if (pInfoArray != null) 
            {
                foreach( ParameterInfo paramInfo in pInfoArray )
                {
                    // See if the ParamArray attribute is defined.
                    bool isDef = Attribute.IsDefined( 
                        paramInfo, typeof(ParamArrayAttribute));

                    if( isDef )
                        Console.WriteLine( 
                            "\nThe ParamArray attribute is defined " +
                            "for \nparameter {0} of method {1}.",
                            paramInfo.Name, mInfo.Name);

                    // See if ParamUsageAttribute is defined.  
                    // If so, display a message.
                    ArgumentUsageAttribute usageAttr = (ArgumentUsageAttribute)
                        Attribute.GetCustomAttribute( 
                            paramInfo, typeof(ArgumentUsageAttribute) );

                    if( usageAttr != null )
                    {
                        Console.WriteLine( 
                            "\nThe ArgumentUsage attribute is defined " +
                            "for \nparameter {0} of method {1}.",
                            paramInfo.Name, mInfo.Name );

                        Console.WriteLine( "\n    The usage " +
                            "message for {0} is:\n    \"{1}\".",
                            paramInfo.Name, usageAttr.Message);
                    }
                }
            }
            else
                Console.WriteLine( 
                    "The parameters information could not " +
                    "be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
This example of Attribute.GetCustomAttribute( ParameterInfo, Type )
generates the following output.

The ArgumentUsage attribute is defined for
parameter strArray of method TestMethod.

    The usage message for strArray is:
    "Must pass an array here.".

The ParamArray attribute is defined for
parameter strList of method TestMethod.

The ArgumentUsage attribute is defined for
parameter strList of method TestMethod.

    The usage message for strList is:
    "Can pass a parameter list or array here.".
*/

[C++] 
// Example for the Attribute::GetCustomAttribute( ParameterInfo*, Type* ) 
// method.
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Reflection;

#define null (Object*)0

namespace NDP_UE_CPP
{
    // Define a custom parameter attribute that takes a single message argument.
    [attribute( Parameter )]
    public __gc class ArgumentUsageAttribute : public Attribute
    {
        // usageMsg is storage for the attribute message.
        protected: 
            String* usageMsg;

        // This is the attribute constructor.
        public:
            ArgumentUsageAttribute( String* UsageMsg )
            {
                this->usageMsg = UsageMsg;
            }

            // This is the Message property for the attribute.
            __property String* get_Message( )
            {
                return usageMsg;
            }
            __property void set_Message( String* value )
            {
                this->usageMsg = value; 
            }
    };

    public __gc class BaseClass 
    {
        // Assign an ArgumentUsage attribute to the strArray parameter.
        // Assign a ParamArray attribute to strList.
        public:
            virtual void TestMethod(
                [ArgumentUsage(S"Must pass an array here.")]
                String* strArray[ ],
                [ParamArray]
                String* strList[ ] )
            { }
    };

    public __gc class DerivedClass : public BaseClass
    {
        // Assign an ArgumentUsage attributes to the strList parameter.
        public:
            void TestMethod(
                String* strArray[ ],
                [ArgumentUsage(
                    S"Can pass a parameter list or array here.")]
                String* strList[ ] )
            { }
    };
}

void main( ) 
{
    Console::WriteLine( 
        S"This example of Attribute::GetCustomAttribute( Param"
        S"eterInfo*, Type* )\ngenerates the following output." );

    // Get the class type, and then get the MethodInfo object 
    // for TestMethod to access its metadata.
    Type* clsType = __typeof( NDP_UE_CPP::DerivedClass );
    MethodInfo* mInfo = clsType->GetMethod( S"TestMethod" );

    // Iterate through the ParameterInfo array for the method parameters.
    ParameterInfo* pInfoArray[] = mInfo->GetParameters();
    if( pInfoArray != null ) 
    {
        // This implements foreach( ParameterInfo* paramInfo in pInfoArray ).
        IEnumerator*    myEnum = pInfoArray->GetEnumerator( );
        while( myEnum->MoveNext( ) )
        {
            ParameterInfo* paramInfo = 
                __try_cast<ParameterInfo*>( myEnum->Current );

            // See if the ParamArray attribute is defined.
            bool isDef = Attribute::IsDefined( paramInfo, 
                __typeof( ParamArrayAttribute ) );

            if( isDef )
                Console::WriteLine(
                    S"\nThe ParamArray attribute is defined for \n" 
                    S"parameter {0} of method {1}.",
                    paramInfo->Name, mInfo->Name);

            // See if ParamUsageAttribute is defined.  
            // If so, display a message.
            NDP_UE_CPP::ArgumentUsageAttribute* usageAttr = 
                static_cast<NDP_UE_CPP::ArgumentUsageAttribute*>(
                    Attribute::GetCustomAttribute( paramInfo, 
                        __typeof(NDP_UE_CPP::ArgumentUsageAttribute) ) );

            if( usageAttr != null )
            {
                Console::WriteLine( 
                    S"\nThe ArgumentUsage attribute is defined for \n" 
                    S"parameter {0} of method {1}.",
                    paramInfo->Name, mInfo->Name );
                Console::WriteLine( S"\n    The usage "
                    S"message for {0} is:\n    \"{1}\".",
                    paramInfo->Name, usageAttr->Message);
            }
        }
    }
    else
        Console::WriteLine( 
            S"The parameters information could " 
            S"not be retrieved for method {0}.", mInfo->Name);
}

/*
This example of Attribute::GetCustomAttribute( ParameterInfo*, Type* )
generates the following output.

The ArgumentUsage attribute is defined for
parameter strArray of method TestMethod.

    The usage message for strArray is:
    "Must pass an array here.".

The ParamArray attribute is defined for
parameter strList of method TestMethod.

The ArgumentUsage attribute is defined for
parameter strList of method TestMethod.

    The usage message for strList is:
    "Can pass a parameter list or array here.".
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Attribute クラス | Attribute メンバ | System 名前空間 | Attribute.GetCustomAttribute オーバーロードの一覧