MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) MemberInfo.IsDefined(Type, Boolean) Method

Definizione

Quando se ne effettua l'override in una classe derivata, indica se a questo membro sono applicati uno o più attributi del tipo specificato o dei tipi derivati.When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member.

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 Type Type Type

Tipo di attributo personalizzato da cercare.The type of custom attribute to search for. La ricerca include i tipi derivati.The search includes derived types.

inherit
Boolean Boolean Boolean Boolean

true per eseguire la ricerca nella catena di ereditarietà del membro per trovare gli attributi; in caso contrario, false.true to search this member's inheritance chain to find the attributes; otherwise, false. Questo parametro viene ignorato per proprietà ed eventi.This parameter is ignored for properties and events.

Restituisce

true se una o più istanze di attributeType o qualsiasi suo tipo derivato viene applicato al membro; in caso contrario, false.true if one or more instances of attributeType or any of its derived types is applied to this member; otherwise, false.

Implementazioni

Esempi

Nell'esempio seguente viene determinato se l'attributo specificato viene applicato al membro specificato.The following example determines whether the specified attribute is applied to the specified member.

using namespace System;
using namespace System::Reflection;

// Define a custom attribute with one named parameter.

[AttributeUsage(AttributeTargets::All)]
public ref class MyAttribute: public Attribute
{
private:
   String^ myName;

public:
   MyAttribute( String^ name )
   {
      myName = name;
   }

   property String^ Name 
   {
      String^ get()
      {
         return myName;
      }
   }
};

// Define a class that has the custom attribute associated with one of its members.
public ref class MyClass1
{
public:

   [MyAttribute("This is an example attribute.")]
   void MyMethod( int i ){}
};

int main()
{
   try
   {
      // Get the type of MyClass1.
      Type^ myType = MyClass1::typeid;

      // Get the members associated with MyClass1.
      array<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( MyAttribute::typeid, false ) )
         {
            array<Object^>^myAttributes = myMembers[ i ]->GetCustomAttributes( MyAttribute::typeid, 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}\"",
                        (safe_cast<MyAttribute^>(myAttributes[ j ]))->Name );
         }
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception occurred: {0}", e->Message );
   }
}

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 'MyAttribute

' 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 'MyClass1

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 'MemberInfo_GetCustomAttributes_IsDefined

Commenti

Questo metodo ignora il parametro inherit per le proprietà e gli eventi.This method ignores the inherit parameter for properties and events. Per eseguire una ricerca nella catena di ereditarietà per gli attributi di proprietà ed eventi, usare gli overload appropriati Attribute.IsDefined del metodo.To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute.IsDefined method.

Nota

Nel .NET Framework versione 2,0, questo metodo restituisce true se un tipo, un metodo o un costruttore ha attributi di sicurezza archiviati nel nuovo formato dei metadati.In the .NET Framework version 2.0, this method returns true if a type, method, or constructor has security attributes stored in the new metadata format. Gli assembly compilati con la versione 2,0 usano questo formato.Assemblies compiled with version 2.0 use this format. Gli assembly e gli assembly dinamici compilati con versioni precedenti del .NET Framework utilizzano il vecchio formato XML.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. Vedere creazione di attributi di sicurezza dichiarativi.See Emitting Declarative Security Attributes.

Si applica a