Partager via


ParameterInfo.IsDefined Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Determines whether the custom attribute of the specified type or its derived types is applied to this parameter.

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

Syntax

'Declaration
Public Overridable Function IsDefined ( _
    attributeType As Type, _
    inherit As Boolean _
) As Boolean
public virtual bool IsDefined(
    Type attributeType,
    bool inherit
)

Parameters

  • attributeType
    Type: System.Type
    The type to search for.
  • inherit
    Type: System.Boolean
    This parameter is ignored for objects of this type. See Remarks.

Return Value

Type: System.Boolean
true if one or more instances of attributeType or its derived types are applied to this parameter; otherwise, false.

Implements

ICustomAttributeProvider.IsDefined(Type, Boolean)

Exceptions

Exception Condition
ArgumentNullException

attributeType is nulla null reference (Nothing in Visual Basic).

ArgumentException

attributeType is not a Type object supplied by the common language runtime.

Remarks

This method ignores the inherit parameter. To search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute.IsDefined method.

Examples

The following example defines two custom attributes, MyAttribute and MyDerivedAttribute. MyDerivedAttribute is derived from MyAttribute. The example then applies these attributes to the parameters of a method of an example class.

When the example is run, it uses the IsDefined method to test all parameters of all methods in the example class. It then displays the parameters that have MyAttribute or MyDerivedAttribute.

Imports System.Reflection

' 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

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

' Derive another custom attribute from MyAttribute
<AttributeUsage(AttributeTargets.Parameter)> _
Public Class MyDerivedAttribute
   Inherits MyAttribute

   Public Sub New(ByVal name As String)
      MyBase.New(name)
   End Sub
End Class

' Define a class with a method that has three parameters. Apply
' MyAttribute to one parameter, MyDerivedAttribute to another, and
' no attributes to the third. 
Public Class MyClass1

   Public Sub MyMethod(<MyAttribute("This is an example parameter attribute")> _
                       ByVal i As Integer, _
                       <MyDerivedAttribute("This is another parameter attribute")> _
                       ByVal j As Integer, _
                       ByVal k As Integer)
      Return
   End Sub
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' 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()

      ' For each method of the class 'MyClass1', display all the parameters
      ' to which MyAttribute or its derived types have been applied.
      For Each mi As MethodInfo In myMethods
         ' Get the parameters for the method.
         Dim myParameters As ParameterInfo() = mi.GetParameters()
         If myParameters.Length > 0 Then
            outputBlock.Text &= String.Format(vbCrLf & "The following parameters of {0} have MyAttribute or a derived type: ", mi) & vbCrLf
            For Each pi As ParameterInfo In myParameters
               If pi.IsDefined(GetType(MyAttribute), False) Then
                  outputBlock.Text &= String.Format("Parameter {0}, name = {1}, type = {2}", _
                      pi.Position, pi.Name, pi.ParameterType) & vbCrLf
               End If
            Next
         End If
      Next
   End Sub
End Class

' This code example produces the following output:
'
'The following parameters of Void MyMethod(Int32, Int32, Int32) have MyAttribute or a derived type:
'Parameter 0, name = i, type = System.Int32
'Parameter 1, name = j, type = System.Int32
'
'The following parameters of Boolean Equals(System.Object) have MyAttribute or a derived type:
using System;
using System.Reflection;

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

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

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

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Get the type of the class 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the members associated with the class 'MyClass1'.
      MethodInfo[] myMethods = myType.GetMethods();

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

/* This code example produces the following output:

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

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.