Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute.IsDefined Method (ParameterInfo, Type)

Determines whether any custom attributes are applied to a method parameter. Parameters specify the method parameter, and the type of the custom attribute to search for.

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

public static bool IsDefined (
	ParameterInfo element,
	Type attributeType
)
public static boolean IsDefined (
	ParameterInfo element, 
	Type attributeType
)
public static function IsDefined (
	element : ParameterInfo, 
	attributeType : Type
) : boolean

Parameters

element

An object derived from the ParameterInfo class that describes a parameter of a member of a class.

attributeType

The type, or a base type, of the custom attribute to search for.

Return Value

true if a custom attribute of type attributeType is applied to element; otherwise, false.

Exception typeCondition

ArgumentNullException

element or attributeType is a null reference (Nothing in Visual Basic).

ArgumentException

attributeType is not derived from Attribute.

The ancestors of element are searched for custom attributes.

The following code example illustrates the use of IsDefined, taking a ParameterInfo as a parameter.

using System;
using System.Reflection;

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

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

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

package IsDef5CS;

import System.*;
import System.Reflection.*;

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

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

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

import System;
import System.Reflection;

package IsDef5JS {
    public class TestClass {
        // Assign a ParamArray attribute to the parameter.
        public function Method1(... args : String[]) : void
        {}
    }

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft