ParameterInfo.IsDefined(Type, Boolean) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Bepaalt of het aangepaste kenmerk van het opgegeven type of de afgeleide typen wordt toegepast op deze 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
Parameters
- attributeType
- Type
Het Type object waarnaar moet worden gezocht.
- inherit
- Boolean
Dit argument wordt genegeerd voor objecten van dit type.
Retouren
true als een of meer exemplaren van attributeType of de afgeleide typen worden toegepast op deze parameter; falseanders.
Implementeringen
Uitzonderingen
attributeType is null.
attributeType is geen Type object dat wordt geleverd door de algemene taalruntime.
Voorbeelden
In het volgende voorbeeld worden twee aangepaste kenmerken gedefinieerd en MyAttributeMyDerivedAttribute.
MyDerivedAttribute is afgeleid van MyAttribute. In het voorbeeld worden deze kenmerken vervolgens toegepast op de parameters van een methode van een voorbeeldklasse.
Wanneer het voorbeeld wordt uitgevoerd, wordt de IsDefined methode gebruikt om alle parameters van alle methoden in de voorbeeldklasse te testen. Vervolgens worden de parameters weergegeven met MyAttribute of MyDerivedAttribute.
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:
Opmerkingen
Met deze methode wordt de inherit parameter genegeerd. Als u de overnameketen wilt doorzoeken op kenmerken op parameters, gebruikt u de juiste overbelasting van de Attribute.IsDefined methode.