Freigeben über


ParameterInfo.IsDefined-Methode

Bestimmt, ob das benutzerdefinierte Attribut vom angegebenen Typ für diesen Member definiert ist.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Function IsDefined ( _
    attributeType As Type, _
    inherit As Boolean _
) As Boolean
'Usage
Dim instance As ParameterInfo
Dim attributeType As Type
Dim inherit As Boolean
Dim returnValue As Boolean

returnValue = instance.IsDefined(attributeType, inherit)
public virtual bool IsDefined (
    Type attributeType,
    bool inherit
)
public:
virtual bool IsDefined (
    Type^ attributeType, 
    bool inherit
)
public boolean IsDefined (
    Type attributeType, 
    boolean inherit
)
public function IsDefined (
    attributeType : Type, 
    inherit : boolean
) : boolean

Parameter

  • attributeType
    Das Type-Objekt, nach dem gesucht werden soll.
  • inherit
    Dieses Argument wird für Objekte dieses Typs ignoriert.

Rückgabewert

true, wenn eine oder mehrere Instanzen von attributeType für diesen Member definiert sind, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

attributeType ist NULL (Nothing in Visual Basic).

Beispiel

'  The following example shows how the attributes related to the parameters of methods 
'  of the class can be retrieved at run time. A custom parameter level attribute 
'  named 'MyAttribute' is defined. This custom attribute is associated with a 
'  parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then 
'  retrieved at run time for the parameter of the method of the class 'MyClass1' 
'  and displayed.

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.Parameter)> Public Class MyAttribute
    Inherits Attribute
    Private myName As String

    Public Sub New(ByVal name As String)
        myName = name
    End Sub 'New

    Public ReadOnly Property Name() As String
        Get
            Return myName
        End Get
    End Property
End Class 'MyAttribute

' Define a class which has a custom attribute associated with one of parameters of a method. 
Public Class MyClass1

    Public Sub MyMethod(<MyAttribute("This is an example parameter attribute")> ByVal i As Integer)
        Return
    End Sub 'MyMethod
End Class 'MyClass1


Public Class MemberInfo_GetCustomAttributes

    Public Shared Sub Main()
        ' Get the type of the class 'MyClass1'.
        Dim myType As Type = GetType(MyClass1)
        ' Get the members associated with the class 'MyClass1'.
        Dim myMethods As MethodInfo() = myType.GetMethods()

        ' Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
        Dim i As Integer
        For i = 0 To myMethods.Length - 1
            ' Get the parameters for the method.
            Dim myParameters As ParameterInfo() = myMethods(i).GetParameters()
            Console.WriteLine(ControlChars.Cr & "The parameters for the method {0} that have a custom attribute are : " & ControlChars.Cr, myMethods(i))
            Dim j As Integer
            For j = 0 To myParameters.Length - 1
                ' Get the attributes of type 'MyAttribute' for each parameter.
                Dim myAttributes As [Object]() = myParameters(j).GetCustomAttributes(GetType(MyAttribute), False)
                If myParameters(j).IsDefined(GetType(MyAttribute), False) Then
                    Console.WriteLine(ControlChars.Cr + "The attributes for the parameter ""{0}"" are : " & ControlChars.Cr, myParameters(j).ParameterType)
                    ' Display all the attributes of type 'MyAttribute' for a parameter.
                    Dim k As Integer
                    For k = 0 To myAttributes.Length - 1
                        Console.WriteLine("The type of the attribute is : {0}", myAttributes(k))
                    Next k
                End If
            Next j
        Next i
    End Sub 'Main
End Class 'MemberInfo_GetCustomAttributes 
// The following example shows how the attributes related to the parameters of methods 
// of the class can be retrieved at run time. A custom parameter level attribute 
// named 'MyAttribute' is defined. This custom attribute is associated with a 
// parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then 
// retrieved at run time for the parameter of the method of the class 'MyClass1' 
// and displayed.

using System;
using System.Reflection;

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

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

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

        // Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
        for(int i = 0; i < myMethods.Length; i++)
        {
            // Get the parameters for the method.
            ParameterInfo[] myParameters = myMethods[i].GetParameters();
            Console.WriteLine("\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[i]);
            for(int j = 0; j < myParameters.Length; j++)
            {
                // Get the attributes of type 'MyAttribute' for each parameter.
                Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);
                if(myParameters[j].IsDefined(typeof(MyAttribute), false))
                {
                    Console.WriteLine("\nThe attributes for the parameter \"{0}\" are : \n", myParameters[j].ParameterType);
                    // Display all the attributes of type 'MyAttribute' for a parameter.
                    for(int k = 0; k < myAttributes.Length; k++)
                        Console.WriteLine("The type of the attribute is : {0}", myAttributes[k]);
                }
            }
        }  
    }
}
// System::Reflection::ParameterInfo::GetCustomAttributes(Type, bool)
// System::Reflection::ParameterInfo::IsDefined(Type, bool)
/*
   This example shows how the attributes related to the parameters of methods 
   of the class can be retrieved at run time. A custom parameter level attribute 
   named 'MyAttribute' is defined. This custom attribute is associated with a 
   parameter of the method 'MyMethod' of 'MyClass1'. This attribute is then 
   retrieved at run time for the parameter of the method of the class 'MyClass1' 
   and displayed.
 */
using namespace System;
using namespace System::Reflection;

// Define a custom attribute with one named parameter.

[AttributeUsage(AttributeTargets::Parameter)]
public ref class MyAttribute: public Attribute
{
private:
   String^ myName;

public:
   MyAttribute( String^ name )
   {
      myName = name;
   }

   property String^ Name 
   {
      String^ get()
      {
         return myName;
      }
   }
};

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

};

void main()
{
   // Get the type of the class 'MyClass1'.
   Type^ myType = MyClass1::typeid;

   // Get the members associated with the class 'MyClass1'.
   array<MethodInfo^>^myMethods = myType->GetMethods();

   // Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
   for ( int i = 0; i < myMethods->Length; i++ )
   {
      // Get the parameters for the method.
      array<ParameterInfo^>^myParameters = myMethods[ i ]->GetParameters();
      Console::WriteLine( "\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[ i ] );
      for ( int j = 0; j < myParameters->Length; j++ )
      {
         // Get the attributes of type 'MyAttribute' for each parameter.
         array<Object^>^myAttributes = myParameters[ j ]->GetCustomAttributes( MyAttribute::typeid, false );
         if ( myParameters[ j ]->IsDefined( MyAttribute::typeid, false ) )
         {
            Console::WriteLine( "\nThe attributes for the parameter \"{0}\" are : \n", myParameters[ j ]->ParameterType );

            // Display all the attributes of type 'MyAttribute' for a parameter.
            for ( int k = 0; k < myAttributes->Length; k++ )
               Console::WriteLine( "The type of the attribute is : {0}", myAttributes[ k ] );
         }
      }
   }
}
// The following example shows how the attributes related to the parameters 
// of methods of the class can be retrieved at run time. A custom parameter 
// level attribute named 'MyAttribute' is defined. This custom attribute is 
// associated with a parameter of the method 'MyMethod' of 'MyClass1'. This
// attribute is then retrieved at run time for the parameter of the method
// of the class 'MyClass1' and displayed.

import System.*;  
import System.Reflection.*;  
// Define a custom attribute with one named parameter.
/** @attribute AttributeUsage(AttributeTargets.Parameter)
 */
public class MyAttribute extends Attribute
{
    private String myName;

    public MyAttribute(String name)
    {
        myName = name;
    } //MyAttribute

    /** @property 
     */
    public String get_Name()
    {
        return myName;
    } //get_Name
} //MyAttribute

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

public class MemberInfo_GetCustomAttributes
{
    public static void main(String[] args)
    {
        // Get the type of the class 'MyClass1'.
        Type myType = MyClass1.class.ToType();
        // Get the members associated with the class 'MyClass1'.
        MethodInfo myMethods[] = myType.GetMethods();
        // Display the attributes of type 'MyAttribute' for each 
        // of the parameters of each method of the class 'MyClass1'.
        for (int i = 0; i < myMethods.get_Length(); i++) {
            // Get the parameters for the method.
            ParameterInfo myParameters[] = myMethods[i].GetParameters();
            Console.WriteLine("\nThe parameters for the method \"{0}\" "
                + "which have an custom attribute are : \n",
                myMethods.get_Item(i));
            for (int j = 0; j < myParameters.get_Length(); j++) {
                // Get the attributes of type 'MyAttribute' for each parameter.
                Object myAttributes[] = myParameters[j].
                    GetCustomAttributes(MyAttribute.class.ToType(), false);
                if (myParameters[j].IsDefined(MyAttribute.class.ToType(),
                    false)) {
                    Console.WriteLine("\nThe attributes for the parameter"
                        + " \"{0}\" are : \n", myParameters[j].
                        get_ParameterType());
                    // Display all the attributes of type 'MyAttribute' for 
                    // a parameter.
                    for (int k = 0; k < myAttributes.get_Length(); k++) {
                        Console.WriteLine("The type of the attribute is : {0}",
                            myAttributes.get_Item(k));
                    }
                }
            }
        }
    } //main
} //MemberInfo_GetCustomAttributes

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ParameterInfo-Klasse
ParameterInfo-Member
System.Reflection-Namespace