ParameterInfo.IsDefined(Type, Boolean) 方法
定义
确定指定类型或其派生类型的自定义属性是否应用于此参数。Determines whether the custom attribute of the specified type or its derived types is applied to this parameter.
public:
virtual bool IsDefined(Type ^ attributeType, bool inherit);
public virtual bool IsDefined (Type attributeType, bool inherit);
abstract member IsDefined : Type * bool -> bool
override this.IsDefined : Type * bool -> bool
Public Overridable Function IsDefined (attributeType As Type, inherit As Boolean) As Boolean
参数
- attributeType
- Type
要搜索的 Type 对象。The Type object to search for.
- inherit
- Boolean
对于该类型的对象,将忽略此自变量。This argument is ignored for objects of this type.
返回
如果将 attributeType 或其派生类型的一个或多个实例应用于此参数,则为 true;否则为 false。true if one or more instances of attributeType or its derived types are applied to this parameter; otherwise, false.
实现
例外
attributeType 为 null。attributeType is null.
attributeType 不是一个由公共语言运行时提供的 Type 对象。attributeType is not a Type object supplied by the common language runtime.
示例
下面的示例定义了两个自定义特性: MyAttribute 和 MyDerivedAttribute 。The following example defines two custom attributes, MyAttribute and MyDerivedAttribute. MyDerivedAttribute 派生自 MyAttribute。MyDerivedAttribute is derived from MyAttribute. 然后,该示例将这些特性应用于示例类的方法的参数。The example then applies these attributes to the parameters of a method of an example class.
运行该示例时,它使用 IsDefined 方法来测试示例类中所有方法的所有参数。When the example is run, it uses the IsDefined method to test all parameters of all methods in the example class. 然后,它显示具有或的 MyAttribute 参数 MyDerivedAttribute 。It then displays the parameters that have MyAttribute or MyDerivedAttribute.
// System::Reflection::ParameterInfo::GetCustomAttributes(Type, bool)
// System::Reflection::ParameterInfo::IsDefined(Type, bool)
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;
}
}
};
// Derive another custom attribute from MyAttribute.
[AttributeUsage(AttributeTargets::Parameter)]
public ref class MyDerivedAttribute: public MyAttribute
{
public:
MyDerivedAttribute( String^ name ) : MyAttribute( 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 ref class MyClass1
{
public:
void MyMethod( [MyAttribute("This is an example parameter attribute")]
int i,
[MyDerivedAttribute("This is another parameter attribute")]
int j,
int k ){}
};
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();
// For each method of the class 'MyClass1', display all the parameters
// to which MyAttribute or its derived types have been applied.
for each ( MethodInfo^ mi in myMethods )
{
// Get the parameters for the method.
array<ParameterInfo^>^ myParameters = mi->GetParameters();
if ( myParameters->Length > 0 )
{
Console::WriteLine("\nThe following parameters of {0} have MyAttribute or a derived type:", mi);
for each ( ParameterInfo^ pi in myParameters)
{
if (pi->IsDefined(MyAttribute::typeid, false))
{
Console::WriteLine("Parameter {0}, name = {1}, type = {2}",
pi->Position, pi->Name, pi->ParameterType);
}
}
}
}
}
/* 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 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();
// 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)
{
Console.WriteLine("\nThe following parameters of {0} have MyAttribute or a derived type: ", mi);
foreach (ParameterInfo pi in myParameters)
{
if (pi.IsDefined(typeof(MyAttribute), false))
{
Console.WriteLine("Parameter {0}, name = {1}, type = {2}",
pi.Position, pi.Name, pi.ParameterType);
}
}
}
}
}
}
/* 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:
*/
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 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()
' 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
Console.WriteLine(vbCrLf & "The following parameters of {0} have MyAttribute or a derived type: ", mi)
For Each pi As ParameterInfo In myParameters
If pi.IsDefined(GetType(MyAttribute), False) Then
Console.WriteLine("Parameter {0}, name = {1}, type = {2}", _
pi.Position, pi.Name, pi.ParameterType)
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:
注解
此方法将忽略 inherit 参数。This method ignores the inherit parameter. 若要在继承链中搜索参数上的属性,请使用方法的适当重载 Attribute.IsDefined 。To search the inheritance chain for attributes on parameters, use the appropriate overloads of the Attribute.IsDefined method.