Attribute.GetCustomAttribute Attribute.GetCustomAttribute Attribute.GetCustomAttribute Attribute.GetCustomAttribute Method

정의

어셈블리, 모듈, 형식 멤버 또는 메서드 매개 변수에 적용된 지정된 형식의 사용자 지정 특성을 검색합니다.Retrieves a custom attribute of a specified type applied to an assembly, module, type member, or method parameter.

오버로드

GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean)

메서드 매개 변수에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a method parameter. 매개 변수에는 메서드 매개 변수, 검색할 사용자 지정 특성 형식 및 상위 메서드 매개 변수의 검색 여부를 지정합니다.Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean)

형식 멤버에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a member of a type. 매개 변수에는 멤버, 검색할 사용자 지정 특성의 형식 및 상위 멤버의 검색 여부를 지정합니다.Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean)

어셈블리에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to an assembly. 매개 변수에는 어셈블리, 검색할 사용자 지정 특성의 형식 및 무시된 검색 옵션을 지정합니다.Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean)

모듈에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a module. 매개 변수에는 모듈, 검색할 사용자 지정 특성의 형식 및 무시된 검색 옵션을 지정합니다.Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type)

모듈에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a module. 매개 변수에는 모듈과 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the module, and the type of the custom attribute to search for.

GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type)

형식 멤버에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a member of a type. 매개 변수에는 멤버와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the member, and the type of the custom attribute to search for.

GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type)

지정된 어셈블리에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a specified assembly. 매개 변수에는 어셈블리와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the assembly and the type of the custom attribute to search for.

GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type)

메서드 매개 변수에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a method parameter. 매개 변수에는 메서드 매개 변수와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the method parameter, and the type of the custom attribute to search for.

GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean) GetCustomAttribute(ParameterInfo, Type, Boolean)

메서드 매개 변수에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a method parameter. 매개 변수에는 메서드 매개 변수, 검색할 사용자 지정 특성 형식 및 상위 메서드 매개 변수의 검색 여부를 지정합니다.Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::ParameterInfo ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.ParameterInfo * Type * bool -> Attribute
Public Shared Function GetCustomAttribute (element As ParameterInfo, attributeType As Type, inherit As Boolean) As Attribute

매개 변수

element
ParameterInfo ParameterInfo ParameterInfo ParameterInfo

클래스의 멤버 매개 변수를 설명하는 ParameterInfo 클래스에서 파생된 개체입니다.An object derived from the ParameterInfo class that describes a parameter of a member of a class.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

true이면 사용자 지정 특성에 대한 상위 element도 검색함을 지정합니다.If true, specifies to also search the ancestors of element for custom attributes.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

사용자 지정 특성 형식을 로드할 수 없는 경우A custom attribute type cannot be loaded.

예제

다음 코드 예제에서는 사용자 지정 매개 변수를 정의 Attribute 클래스 및 파생된 클래스 및 파생된 클래스의 기본 클래스의 메서드에 사용자 지정 특성을 적용 합니다.The following code example defines a custom parameter Attribute class and applies the custom attribute to a method in a derived class and the base of the derived class. 이 예제에서는 사용을 보여 줍니다.는 GetCustomAttribute 특성을 반환 하는 방법입니다.The example shows the use of the GetCustomAttribute method to return the attributes.

// Example for the Attribute::GetCustomAttribute( ParameterInfo*, Type*, bool ) 
// method.
using namespace System;
using namespace System::Collections;
using namespace System::Reflection;

namespace NDP_UE_CPP
{
   // Define a custom parameter attribute that takes a single message argument.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentUsageAttribute: public Attribute
   {
   protected:

      // usageMsg is storage for the attribute message.
      String^ usageMsg;

   public:

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

      property String^ Message 
      {
         // This is the Message property for the attribute.
         String^ get()
         {
            return usageMsg;
         }

         void set( String^ value )
         {
            this->usageMsg = value;
         }
      }
   };

   public ref class BaseClass
   {
   public:

      // Assign an ArgumentUsage attribute to the strArray parameter.
      // Assign a ParamArray attribute to strList.
      virtual void TestMethod( [ArgumentUsage("Must pass an array here.")]array<String^>^strArray,
                               ...array<String^>^strList ){}
   };

   public ref class DerivedClass: public BaseClass
   {
   public:

      // Assign an ArgumentUsage attributes to the strList parameter.
      virtual void TestMethod( array<String^>^strArray, [ArgumentUsage(
      "Can pass a parameter list or array here.")]array<String^>^strList ) override {}

   };

   void DisplayParameterAttributes( MethodInfo^ mInfo, array<ParameterInfo^>^pInfoArray, bool includeInherited )
   {
      Console::WriteLine( "\nParameter attribute information for method \"{0}"
      "\"\nincludes inheritance from base class: {1}.", mInfo->Name, includeInherited ? (String^)"Yes" : "No" );
      
      // This implements foreach( ParameterInfo* paramInfo in pInfoArray ).
      IEnumerator^ myEnum = pInfoArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         ParameterInfo^ paramInfo = safe_cast<ParameterInfo^>(myEnum->Current);

         // See if the ParamArray attribute is defined.
         bool isDef = Attribute::IsDefined( paramInfo, ParamArrayAttribute::typeid );
         if ( isDef )
                  Console::WriteLine( "\n    The ParamArray attribute is defined "
         "for \n    parameter {0} of method {1}.", paramInfo->Name, mInfo->Name );

         // See if ParamUsageAttribute is defined.  
         // If so, display a message.
         ArgumentUsageAttribute^ usageAttr = static_cast<ArgumentUsageAttribute^>(Attribute::GetCustomAttribute( paramInfo, ArgumentUsageAttribute::typeid, includeInherited ));
         if ( usageAttr != nullptr )
         {
            Console::WriteLine( "\n    The ArgumentUsage attribute is defined "
            "for \n    parameter {0} of method {1}.", paramInfo->Name, mInfo->Name );
            Console::WriteLine( "\n        The usage "
            "message for {0} is:\n        \"{1}\".", paramInfo->Name, usageAttr->Message );
         }
      }
   }

}

int main()
{
   Console::WriteLine( "This example of Attribute::GetCustomAttribute( ParameterInfo*, "
   "Type*, bool )\ngenerates the following output." );

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

   // Iterate through the ParameterInfo array for the method parameters.
   array<ParameterInfo^>^pInfoArray = mInfo->GetParameters();
   if ( pInfoArray != nullptr )
   {
      NDP_UE_CPP::DisplayParameterAttributes( mInfo, pInfoArray, false );
      NDP_UE_CPP::DisplayParameterAttributes( mInfo, pInfoArray, true );
   }
   else
      Console::WriteLine( "The parameters information could "
   "not be retrieved for method {0}.", mInfo->Name );
}

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

Parameter attribute information for method "TestMethod"
includes inheritance from base class: No.

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

Parameter attribute information for method "TestMethod"
includes inheritance from base class: Yes.

    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.".
*/
// Example for the Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean ) 
// 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( Parameter" +
                "Info, Type, Boolean )\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) 
            {
                DisplayParameterAttributes( mInfo, pInfoArray, false );
                DisplayParameterAttributes( mInfo, pInfoArray, true );
            }
            else
                Console.WriteLine("The parameters information could " +
                    "not be retrieved for method {0}.", mInfo.Name);
        }

        static void DisplayParameterAttributes( MethodInfo mInfo,
            ParameterInfo[] pInfoArray, bool includeInherited )
        {
            Console.WriteLine( 
                "\nParameter attribute information for method \"" +
                "{0}\"\nincludes inheritance from base class: {1}.", 
                mInfo.Name, includeInherited ? "Yes" : "No" );

            // Display the attribute information for the parameters.
            foreach( ParameterInfo paramInfo in pInfoArray )
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined( paramInfo, 
                    typeof(ParamArrayAttribute));

                if( isDef )
                    Console.WriteLine(
                        "\n    The ParamArray attribute is defined " +
                        "for \n    parameter {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),
                        includeInherited );

                if( usageAttr != null )
                {
                    Console.WriteLine( 
                        "\n    The ArgumentUsage attribute is def" +
                        "ined for \n    parameter {0} of method {1}.",
                        paramInfo.Name, mInfo.Name );

                    Console.WriteLine( "\n        The usage " +
                        "message for {0} is:\n        \"{1}\".",
                        paramInfo.Name, usageAttr.Message);
                }
            }
        }
    }
}

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

Parameter attribute information for method "TestMethod"
includes inheritance from base class: No.

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

Parameter attribute information for method "TestMethod"
includes inheritance from base class: Yes.

    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.".
*/
' Example for the Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean ) 
' 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 the strList parameter.
        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 the strList parameter.
        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

    Class DemoClass
           
        Shared Sub DisplayParameterAttributes(mInfo As MethodInfo, _
            pInfoArray() As ParameterInfo, includeInherited As Boolean)

            Console.WriteLine( vbCrLf & _
                "Parameter attribute information for method ""{0}""" & _
                vbCrLf & "includes inheritance from the base class: {1}.", _
                mInfo.Name, IIf(includeInherited, "Yes", "No"))
              
            ' Display attribute information for the parameters.
            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), _
                        includeInherited)

                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
        End Sub ' DisplayParameterAttributes
       
        Public Shared Sub Main()
            Console.WriteLine( _
                "This example of Attribute.GetCustomAttribute" & _
                "( ParameterInfo, Type, Boolean )" & 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

                DisplayParameterAttributes(mInfo, pInfoArray, False)
                DisplayParameterAttributes(mInfo, pInfoArray, True)
            Else
                Console.WriteLine( _
                    "The parameters information could " & _
                    "not be retrieved for method {0}.", mInfo.Name)
            End If
        End Sub ' Main

    End Class ' DemoClass
End Namespace ' NDP_UE_VB

' This example of Attribute.GetCustomAttribute( ParameterInfo, Type, Boolean )
' generates the following output.
' 
' Parameter attribute information for method "TestMethod"
' includes inheritance from the base class: No.
' 
'     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.".
' 
' Parameter attribute information for method "TestMethod"
' includes inheritance from the base class: Yes.
' 
'     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.".

설명

경우 element 파생 형식의 메서드에 매개 변수를 나타내는 반환 값 재정의 된 기본 메서드에 동일한 매개 변수에 적용 하는 상속 가능한 사용자 지정 특성을 포함 합니다.If element represents a parameter in a method of a derived type, the return value includes the inheritable custom attributes applied to the same parameter in the overridden base methods.

GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean) GetCustomAttribute(MemberInfo, Type, Boolean)

형식 멤버에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a member of a type. 매개 변수에는 멤버, 검색할 사용자 지정 특성의 형식 및 상위 멤버의 검색 여부를 지정합니다.Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::MemberInfo ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (System.Reflection.MemberInfo element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.MemberInfo * Type * bool -> Attribute
Public Shared Function GetCustomAttribute (element As MemberInfo, attributeType As Type, inherit As Boolean) As Attribute

매개 변수

element
MemberInfo MemberInfo MemberInfo MemberInfo

클래스의 생성자, 이벤트, 필드, 메서드 또는 속성 멤버를 설명하는 MemberInfo 클래스에서 파생된 개체입니다.An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

true이면 사용자 지정 특성에 대한 상위 element도 검색함을 지정합니다.If true, specifies to also search the ancestors of element for custom attributes.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

element가 생성자, 메서드, 속성, 이벤트, 형식 또는 필드가 아닌 경우element is not a constructor, method, property, event, type, or field.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

사용자 지정 특성 형식을 로드할 수 없는 경우A custom attribute type cannot be loaded.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 MemberInfo 매개 변수로.The following code example illustrates the use of the GetCustomAttribute method taking a MemberInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // 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, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        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.".
 */
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.".
 */
Imports System
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

설명

참고

.NET Framework 버전 2.0부터,이 메서드 보안 특성을 반환 형식, 메서드 및 생성자 특성을 새 메타 데이터 형식으로 저장 됩니다.Starting with the .NET Framework version 2.0, this method returns security attributes on types, methods, and constructors if the attributes are stored in the new metadata format. 버전 2.0 이상으로 컴파일된 어셈블리에는 새 형식을 사용 합니다.Assemblies compiled with version 2.0 or later use the new format. 동적 어셈블리 및 이전 버전의.NET Framework를 사용 하 여 컴파일된 어셈블리에는 이전 XML 형식을 사용 합니다.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. 참조 선언적 보안 특성 내보내기합니다.See Emitting Declarative Security Attributes.

GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean) GetCustomAttribute(Assembly, Type, Boolean)

어셈블리에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to an assembly. 매개 변수에는 어셈블리, 검색할 사용자 지정 특성의 형식 및 무시된 검색 옵션을 지정합니다.Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::Assembly ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (System.Reflection.Assembly element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.Assembly * Type * bool -> Attribute
Public Shared Function GetCustomAttribute (element As Assembly, attributeType As Type, inherit As Boolean) As Attribute

매개 변수

element
Assembly Assembly Assembly Assembly

다시 사용 가능한 모듈 컬렉션을 설명하는 Assembly 클래스에서 파생된 개체입니다.An object derived from the Assembly class that describes a reusable collection of modules.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

이 매개 변수는 무시되며 이 메서드의 작업에 영향을 끼치지 않습니다.This parameter is ignored, and does not affect the operation of this method.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 Assembly 매개 변수로 합니다.The following code example illustrates the use of the GetCustomAttribute method taking an Assembly as a parameter.

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         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 = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        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 attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
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".
 */
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

설명

참고

.NET Framework 버전 2.0부터이 메서드는 보안 특성 특성은 새 메타 데이터 형식에 저장 된 경우Starting with the .NET Framework version 2.0, this method returns security attributes if the attributes are stored in the new metadata format. 버전 2.0 이상으로 컴파일된 어셈블리에는 새 형식을 사용 합니다.Assemblies compiled with version 2.0 or later use the new format. 동적 어셈블리 및 이전 버전의.NET Framework를 사용 하 여 컴파일된 어셈블리에는 이전 XML 형식을 사용 합니다.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. 참조 선언적 보안 특성 내보내기합니다.See Emitting Declarative Security Attributes.

GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean) GetCustomAttribute(Module, Type, Boolean)

모듈에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a module. 매개 변수에는 모듈, 검색할 사용자 지정 특성의 형식 및 무시된 검색 옵션을 지정합니다.Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::Module ^ element, Type ^ attributeType, bool inherit);
public static Attribute GetCustomAttribute (System.Reflection.Module element, Type attributeType, bool inherit);
static member GetCustomAttribute : System.Reflection.Module * Type * bool -> Attribute
Public Shared Function GetCustomAttribute (element As Module, attributeType As Type, inherit As Boolean) As Attribute

매개 변수

element
Module Module Module Module

이식 가능한 실행 파일을 설명하는 Module 클래스에서 파생된 개체입니다.An object derived from the Module class that describes a portable executable file.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

이 매개 변수는 무시되며 이 메서드의 작업에 영향을 끼치지 않습니다.This parameter is ignored, and does not affect the operation of this method.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 Module 매개 변수로.The following code example illustrates the use of the GetCustomAttribute method taking a Module as a parameter.

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               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.
 */
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.
 */
Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type) GetCustomAttribute(Module, Type)

모듈에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a module. 매개 변수에는 모듈과 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the module, and the type of the custom attribute to search for.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::Module ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (System.Reflection.Module element, Type attributeType);
static member GetCustomAttribute : System.Reflection.Module * Type -> Attribute
Public Shared Function GetCustomAttribute (element As Module, attributeType As Type) As Attribute

매개 변수

element
Module Module Module Module

이식 가능한 실행 파일을 설명하는 Module 클래스에서 파생된 개체입니다.An object derived from the Module class that describes a portable executable file.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 Module 매개 변수로.The following code example illustrates the use of the GetCustomAttribute method taking a Module as a parameter.

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               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.
 */
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.
 */
Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type) GetCustomAttribute(MemberInfo, Type)

형식 멤버에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a member of a type. 매개 변수에는 멤버와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the member, and the type of the custom attribute to search for.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::MemberInfo ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (System.Reflection.MemberInfo element, Type attributeType);
static member GetCustomAttribute : System.Reflection.MemberInfo * Type -> Attribute
Public Shared Function GetCustomAttribute (element As MemberInfo, attributeType As Type) As Attribute

매개 변수

element
MemberInfo MemberInfo MemberInfo MemberInfo

클래스의 생성자, 이벤트, 필드, 메서드 또는 속성 멤버를 설명하는 MemberInfo 클래스에서 파생된 개체입니다.An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

element가 생성자, 메서드, 속성, 이벤트, 형식 또는 필드가 아닌 경우element is not a constructor, method, property, event, type, or field.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

사용자 지정 특성 형식을 로드할 수 없는 경우A custom attribute type cannot be loaded.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 MemberInfo 매개 변수로.The following code example illustrates the use of the GetCustomAttribute method taking a MemberInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // 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, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        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.".
 */
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.".
 */
Imports System
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

설명

반환 값 섹션에서 설명한 동일한 방식으로 일치 Type.IsAssignableFrom합니다.A match is determined in the same way described in the Return Value section of Type.IsAssignableFrom.

참고

.NET Framework 버전 2.0부터,이 메서드 보안 특성을 반환 형식, 메서드 및 생성자 특성을 새 메타 데이터 형식으로 저장 됩니다.Starting with the .NET Framework version 2.0, this method returns security attributes on types, methods, and constructors if the attributes are stored in the new metadata format. 버전 2.0 이상으로 컴파일된 어셈블리에는 새 형식을 사용 합니다.Assemblies compiled with version 2.0 or later use the new format. 동적 어셈블리 및 이전 버전의.NET Framework를 사용 하 여 컴파일된 어셈블리에는 이전 XML 형식을 사용 합니다.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. 참조 선언적 보안 특성 내보내기합니다.See Emitting Declarative Security Attributes.

GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type) GetCustomAttribute(Assembly, Type)

지정된 어셈블리에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a specified assembly. 매개 변수에는 어셈블리와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the assembly and the type of the custom attribute to search for.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::Assembly ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (System.Reflection.Assembly element, Type attributeType);
static member GetCustomAttribute : System.Reflection.Assembly * Type -> Attribute
Public Shared Function GetCustomAttribute (element As Assembly, attributeType As Type) As Attribute

매개 변수

element
Assembly Assembly Assembly Assembly

다시 사용 가능한 모듈 컬렉션을 설명하는 Assembly 클래스에서 파생된 개체입니다.An object derived from the Assembly class that describes a reusable collection of modules.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

예제

다음 코드 예제를 사용 합니다 GetCustomAttribute 메서드 만들기는 Assembly 매개 변수로 합니다.The following code example illustrates the use of the GetCustomAttribute method taking an Assembly as a parameter.

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         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 = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        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 attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
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".
 */
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

설명

사용 된 GetCustomAttributes 메서드가 반환 될 둘 이상의 값을 예상 하는 경우 또는 AmbiguousMatchException throw 됩니다.Use the GetCustomAttributes method if you expect more than one value to be returned, or AmbiguousMatchException will be thrown.

참고

.NET Framework 버전 2.0부터이 메서드는 보안 특성 특성은 새 메타 데이터 형식에 저장 된 경우Starting with the .NET Framework version 2.0, this method returns security attributes if the attributes are stored in the new metadata format. 버전 2.0 이상으로 컴파일된 어셈블리에는 새 형식을 사용 합니다.Assemblies compiled with version 2.0 or later use the new format. 동적 어셈블리 및 이전 버전의.NET Framework를 사용 하 여 컴파일된 어셈블리에는 이전 XML 형식을 사용 합니다.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. 참조 선언적 보안 특성 내보내기합니다.See Emitting Declarative Security Attributes.

GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type) GetCustomAttribute(ParameterInfo, Type)

메서드 매개 변수에 적용된 사용자 지정 특성을 검색합니다.Retrieves a custom attribute applied to a method parameter. 매개 변수에는 메서드 매개 변수와 검색할 사용자 지정 특성의 형식을 지정합니다.Parameters specify the method parameter, and the type of the custom attribute to search for.

public:
 static Attribute ^ GetCustomAttribute(System::Reflection::ParameterInfo ^ element, Type ^ attributeType);
public static Attribute GetCustomAttribute (System.Reflection.ParameterInfo element, Type attributeType);
static member GetCustomAttribute : System.Reflection.ParameterInfo * Type -> Attribute
Public Shared Function GetCustomAttribute (element As ParameterInfo, attributeType As Type) As Attribute

매개 변수

element
ParameterInfo ParameterInfo ParameterInfo ParameterInfo

클래스의 멤버 매개 변수를 설명하는 ParameterInfo 클래스에서 파생된 개체입니다.An object derived from the ParameterInfo class that describes a parameter of a member of a class.

attributeType
Type Type Type Type

검색할 사용자 지정 특성의 형식, 즉 기본 형식입니다.The type, or a base type, of the custom attribute to search for.

반환

attributeType에 적용된 element 형식의 단일 사용자 지정 특성에 대한 참조입니다. 그러한 특성이 없는 경우에는 null입니다.A reference to the single custom attribute of type attributeType that is applied to element, or null if there is no such attribute.

예외

element 또는 attributeTypenull인 경우element or attributeType is null.

attributeTypeAttribute에서 파생되지 않은 경우attributeType is not derived from Attribute.

요청된 특성이 둘 이상 발견된 경우More than one of the requested attributes was found.

사용자 지정 특성 형식을 로드할 수 없는 경우A custom attribute type cannot be loaded.

예제

다음 코드 예제에서는 사용자 지정 매개 변수를 정의 Attribute 클래스 및 파생된 클래스 및 파생된 클래스의 기본 클래스의 메서드에 사용자 지정 특성을 적용 합니다.The following code example defines a custom parameter Attribute class and applies the custom attribute to a method in a derived class and the base of the derived class. 이 예제에서는 사용을 보여 줍니다.는 GetCustomAttribute 특성을 반환 하는 방법입니다.The example shows the use of the GetCustomAttribute method to return the attributes.

// Example for the Attribute::GetCustomAttribute( ParameterInfo*, Type* ) 
// method.
using namespace System;
using namespace System::Collections;
using namespace System::Reflection;

namespace NDP_UE_CPP
{

   // Define a custom parameter attribute that takes a single message argument.

   [AttributeUsage(AttributeTargets::Parameter)]
   public ref class ArgumentUsageAttribute: public Attribute
   {
   protected:

      // usageMsg is storage for the attribute message.
      String^ usageMsg;

   public:

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


      property String^ Message 
      {
         // This is the Message property for the attribute.
         String^ get()
         {
            return usageMsg;
         }

         void set( String^ value )
         {
            this->usageMsg = value;
         }
      }
   };

   public ref class BaseClass
   {
   public:

      // Assign an ArgumentUsage attribute to the strArray parameter.
      // Assign a ParamArray attribute to strList.
      virtual void TestMethod( [ArgumentUsage("Must pass an array here.")]array<String^>^strArray,
                               ...array<String^>^strList ){}
   };

   public ref class DerivedClass: public BaseClass
   {
   public:

      // Assign an ArgumentUsage attributes to the strList parameter.
      virtual void TestMethod( array<String^>^strArray, [ArgumentUsage(
      "Can pass a parameter list or array here.")]array<String^>^strList ) override {}
   };
}

int 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 = NDP_UE_CPP::DerivedClass::typeid;
   MethodInfo^ mInfo = clsType->GetMethod( "TestMethod" );

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

         // See if the ParamArray attribute is defined.
         bool isDef = Attribute::IsDefined( paramInfo, ParamArrayAttribute::typeid );
         if ( isDef )
                  Console::WriteLine( "\nThe ParamArray attribute is defined for \n"
         "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, NDP_UE_CPP::ArgumentUsageAttribute::typeid ));
         if ( usageAttr != nullptr )
         {
            Console::WriteLine( "\nThe ArgumentUsage attribute is defined for \n"
            "parameter {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.".
*/
// 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.".
*/
' 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.".

설명

경우 element 파생 형식의 메서드에 매개 변수를 나타내는 반환 값 재정의 된 기본 메서드에 동일한 매개 변수에 적용 하는 상속 가능한 사용자 지정 특성을 포함 합니다.If element represents a parameter in a method of a derived type, the return value includes the inheritable custom attributes applied to the same parameter in the overridden base methods.

적용 대상