MemberInfo.IsDefined(Type, Boolean) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
In caso di override in una classe derivata, indica se uno o più attributi del tipo specificato o dei relativi tipi derivati vengono applicati a questo membro.
public:
abstract bool IsDefined(Type ^ attributeType, bool inherit);
public abstract bool IsDefined(Type attributeType, bool inherit);
abstract member IsDefined : Type * bool -> bool
Public MustOverride Function IsDefined (attributeType As Type, inherit As Boolean) As Boolean
Parametri
- attributeType
- Type
Tipo di attributo personalizzato da cercare. La ricerca include tipi derivati.
- inherit
- Boolean
true per eseguire ricerche nella catena di ereditarietà di questo membro per trovare gli attributi; in caso contrario, false. Questo parametro viene ignorato per proprietà ed eventi.
Valori restituiti
true se una o più istanze di attributeType o uno dei relativi tipi derivati vengono applicate a questo membro; in caso contrario, false.
Implementazioni
Esempio
Nell'esempio seguente viene determinato se l'attributo specificato viene applicato al membro specificato.
using System;
using System.Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
// Define a class that has the custom attribute associated with one of its members.
public class MyClass1
{
[MyAttribute("This is an example attribute.")]
public void MyMethod(int i)
{
return;
}
}
public class MemberInfo_GetCustomAttributes_IsDefined
{
public static void Main()
{
try
{
// Get the type of MyClass1.
Type myType = typeof(MyClass1);
// Get the members associated with MyClass1.
MemberInfo[] myMembers = myType.GetMembers();
// Display the attributes for each of the members of MyClass1.
for(int i = 0; i < myMembers.Length; i++)
{
// Display the attribute if it is of type MyAttribute.
if(myMembers[i].IsDefined(typeof(MyAttribute), false))
{
Object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(MyAttribute), false);
Console.WriteLine("\nThe attributes of type MyAttribute for the member {0} are: \n",
myMembers[i]);
for(int j = 0; j < myAttributes.Length; j++)
// Display the value associated with the attribute.
Console.WriteLine("The value of the attribute is : \"{0}\"",
((MyAttribute)myAttributes[j]).Name);
}
}
}
catch(Exception e)
{
Console.WriteLine("An exception occurred: {0}", e.Message);
}
}
}
Imports System.Reflection
' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.All)> 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
' Define a class that has the custom attribute associated with one of its members.
Public Class MyClass1
<MyAttribute("This is an example attribute.")> Public Sub MyMethod(ByVal i As Integer)
Return
End Sub
End Class
Public Class MemberInfo_GetCustomAttributes_IsDefined
Public Shared Sub Main()
Try
' Get the type of MyClass1.
Dim myType As Type = GetType(MyClass1)
' Get the members associated with MyClass1.
Dim myMembers As MemberInfo() = myType.GetMembers()
' Display the attributes for each of the members of MyClass1.
Dim i As Integer
For i = 0 To myMembers.Length - 1
' Display the attribute if it is of type MyAttribute.
If myMembers(i).IsDefined(GetType(MyAttribute), False) Then
Dim myAttributes As [Object]() = myMembers(i).GetCustomAttributes(GetType(MyAttribute), False)
Console.WriteLine(ControlChars.Cr + "The attributes of type MyAttribute for the member {0} are: " + ControlChars.Cr, myMembers(i))
Dim j As Integer
For j = 0 To myAttributes.Length - 1
' Display the value associated with the attribute.
Console.WriteLine("The value of the attribute is : ""{0}""", CType(myAttributes(j), MyAttribute).Name)
Next j
End If
Next i
Catch e As Exception
Console.WriteLine("An exception occurred: {0}", e.Message)
End Try
End Sub
End Class
Commenti
Questo metodo ignora il inherit parametro per proprietà ed eventi. Per cercare nella catena di ereditarietà gli attributi relativi a proprietà ed eventi, usare gli overload appropriati del Attribute.IsDefined metodo .