Attribute.IsDefined Attribute.IsDefined Attribute.IsDefined Attribute.IsDefined Method

Definizione

Stabilisce se vengono applicati attributi personalizzati di un tipo specificato a un assembly, un modulo, un membro del tipo o un parametro del metodo.Determines whether any custom attributes of a specified type are applied to an assembly, module, type member, or method parameter.

Overload

IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo.Determines whether any custom attributes are applied to a method parameter. I parametri consentono di specificare il parametro del metodo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e la ricerca di eventuali predecessori del parametro del metodo.Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un modulo.Determines whether any custom attributes are applied to a module. I parametri consentono di specificare il modulo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo.Determines whether any custom attributes are applied to a member of a type. I parametri consentono di specificare il membro, il tipo dell'attributo personalizzato di cui eseguire la ricerca e la ricerca di eventuali predecessori del membro.Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un assembly.Determines whether any custom attributes are applied to an assembly. I parametri consentono di specificare l'assembly, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo.Determines whether any custom attributes are applied to a member of a type. I parametri consentono di specificare il membro e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the member, and the type of the custom attribute to search for.

IsDefined(Module, Type) IsDefined(Module, Type) IsDefined(Module, Type) IsDefined(Module, Type)

Determina se vengono applicati attributi personalizzati del tipo specificato a un modulo.Determines whether any custom attributes of a specified type are applied to a module. I parametri consentono di specificare il modulo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the module, and the type of the custom attribute to search for.

IsDefined(Assembly, Type) IsDefined(Assembly, Type) IsDefined(Assembly, Type) IsDefined(Assembly, Type)

Stabilisce se vengono applicati attributi personalizzati a un assembly.Determines whether any custom attributes are applied to an assembly. I parametri consentono di specificare l'assembly e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the assembly, and the type of the custom attribute to search for.

IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo.Determines whether any custom attributes are applied to a method parameter. I parametri consentono di specificare il parametro del metodo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the method parameter, and the type of the custom attribute to search for.

IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean) IsDefined(ParameterInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo.Determines whether any custom attributes are applied to a method parameter. I parametri consentono di specificare il parametro del metodo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e la ricerca di eventuali predecessori del parametro del metodo.Parameters specify the method parameter, the type of the custom attribute to search for, and whether to search ancestors of the method parameter.

public:
 static bool IsDefined(System::Reflection::ParameterInfo ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.ParameterInfo * Type * bool -> bool
Public Shared Function IsDefined (element As ParameterInfo, attributeType As Type, inherit As Boolean) As Boolean

Parametri

element
ParameterInfo ParameterInfo ParameterInfo ParameterInfo

Un oggetto derivato dalla classe ParameterInfo che descrive un parametro di un membro di una classe.An object derived from the ParameterInfo class that describes a parameter of a member of a class.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

Se è true, specifica di ricercare anche gli attributi personalizzati nei predecessori di element.If true, specifies to also search the ancestors of element for custom attributes.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

element non è un metodo, un costruttore o un tipo.element is not a method, constructor, or type.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un ParameterInfo come parametro.The following code example illustrates the use of IsDefined, taking a ParameterInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef5CS
{
   public ref class TestClass
   {
   public:

      // Assign a ParamArray attribute to the parameter using the keyword.
      void Method1(... array<String^>^args ){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // Get the ParameterInfo array for the method parameters.
         array<ParameterInfo^>^pInfo = mInfo->GetParameters();
         if ( pInfo != nullptr )
         {
            
            // See if the ParamArray attribute is defined.
            bool isDef = Attribute::IsDefined( pInfo[ 0 ], ParamArrayAttribute::typeid );
            
            // Display the result.
            Console::WriteLine( "The ParamArray attribute {0} defined for "
            "parameter {1} of method {2}.", isDef ? (String^)"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.
 */
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.
 */
Imports System
Imports System.Reflection

Module DemoModule
    Public Class TestClass
        ' Assign a ParamArray attribute to the parameter using the keyword.
        Public Sub Method1(ByVal ParamArray args As String())
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the ParameterInfo array for the method parameters.
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        If Not pInfo(0) Is Nothing Then
            ' See if the ParamArray attribute is defined.
            Dim isDef As Boolean = Attribute.IsDefined(pInfo(0), _
                                   GetType(ParamArrayAttribute))
            Dim strDef As String
            If isDef = True Then
                strDef = "is"
            Else
                strDef = "is not"
            End If
            ' Display the result.
            Console.WriteLine("The ParamArray attribute {0} defined " & _
                              "for parameter {1} of method {2}.", _
                              strDef, pInfo(0).Name, mInfo.Name)
        Else
            Console.WriteLine("Could not retrieve parameter information " & _
                              "for method {0}.", mInfo.Name)
        End If
    End Sub
End Module

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

IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean) IsDefined(Module, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un modulo.Determines whether any custom attributes are applied to a module. I parametri consentono di specificare il modulo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

public:
 static bool IsDefined(System::Reflection::Module ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.Module element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.Module * Type * bool -> bool
Public Shared Function IsDefined (element As Module, attributeType As Type, inherit As Boolean) As Boolean

Parametri

element
Module Module Module Module

Oggetto derivato dalla classe Module che descrive un file eseguibile portabile.An object derived from the Module class that describes a portable executable file.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

Questo parametro viene ignorato e non influisce sul funzionamento di questo metodo.This parameter is ignored, and does not affect the operation of this method.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un Module come parametro.The following code example illustrates the use of IsDefined, taking a Module as a parameter.

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               Console::WriteLine( "JITTrackingEnabled is {0}.", dbgAttr->IsJITTrackingEnabled );
               Console::WriteLine( "JITOptimizerDisabled is {0}.", dbgAttr->IsJITOptimizerDisabled );
            }
            else
                        Console::WriteLine( "The Debuggable attribute "
            "could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module, 
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module, 
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

Commenti

Questo metodo ignora la inherit parametro e non esegue la ricerca i predecessori di element per gli attributi personalizzati.This method ignores the inherit parameter and does not search the ancestors of element for custom attributes.

IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean) IsDefined(MemberInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo.Determines whether any custom attributes are applied to a member of a type. I parametri consentono di specificare il membro, il tipo dell'attributo personalizzato di cui eseguire la ricerca e la ricerca di eventuali predecessori del membro.Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

public:
 static bool IsDefined(System::Reflection::MemberInfo ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.MemberInfo * Type * bool -> bool
Public Shared Function IsDefined (element As MemberInfo, attributeType As Type, inherit As Boolean) As Boolean

Parametri

element
MemberInfo MemberInfo MemberInfo MemberInfo

Un oggetto derivato dalla classe MemberInfo che descrive un costruttore, un evento, un campo, un metodo, un tipo o un membro di proprietà di una classe.An object derived from the MemberInfo class that describes a constructor, event, field, method, type, or property member of a class.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

Se è true, specifica di ricercare anche gli attributi personalizzati nei predecessori di element.If true, specifies to also search the ancestors of element for custom attributes.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

element non è un costruttore, un metodo, una proprietà, un evento, un tipo o un campo.element is not a constructor, method, property, event, type, or field.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un MemberInfo come parametro.The following code example illustrates the use of IsDefined, taking a MemberInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // See if the Obsolete attribute is defined for this method.
         bool isDef = Attribute::IsDefined( mInfo, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        Console::WriteLine( "The message is: \"{0}\".", obsAttr->Message );
            else
                        Console::WriteLine( "The message could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
using System;
using System.Reflection;

namespace IsDef4CS 
{
    public class TestClass 
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    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");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef) 
            {
                ObsoleteAttribute obsAttr = 
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute( 
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
Imports System
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

Commenti

Nota

A partire da .NET Framework versione 2.0, questo metodo restituisce true se un tipo, metodo o costruttore ha gli attributi di sicurezza archiviati nel nuovo formato dei metadati.Starting with 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 o versioni successive usano il nuovo formato.Assemblies compiled with version 2.0 or later use the new format. Assembly dinamici e gli assembly compilati con le versioni precedenti di .NET Framework usano il vecchio formato XML.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. Visualizzare creazione di attributi di sicurezza dichiarativa.See Emitting Declarative Security Attributes.

IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean) IsDefined(Assembly, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un assembly.Determines whether any custom attributes are applied to an assembly. I parametri consentono di specificare l'assembly, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.

public:
 static bool IsDefined(System::Reflection::Assembly ^ element, Type ^ attributeType, bool inherit);
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType, bool inherit);
static member IsDefined : System.Reflection.Assembly * Type * bool -> bool
Public Shared Function IsDefined (element As Assembly, attributeType As Type, inherit As Boolean) As Boolean

Parametri

element
Assembly Assembly Assembly Assembly

Oggetto derivato dalla classe Assembly che descrive una raccolta riutilizzabile di moduli.An object derived from the Assembly class that describes a reusable collection of modules.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

inherit
Boolean Boolean Boolean Boolean

Questo parametro viene ignorato e non influisce sul funzionamento di questo metodo.This parameter is ignored, and does not affect the operation of this method.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un Assembly come parametro.The following code example illustrates the use of IsDefined, taking an Assembly as a parameter.

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         if ( isdef )
         {
            
            // Affirm that the attribute is defined.
            Console::WriteLine( "The AssemblyDescription attribute "
            "is defined for assembly {0}.", assyName );
            
            // Get the description attribute itself.
            AssemblyDescriptionAttribute^ adAttr = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        Console::WriteLine( "The description is \"{0}\".", adAttr->Description );
            else
                        Console::WriteLine( "The description could not "
            "be retrieved." );
         }
         else
                  Console::WriteLine( "The AssemblyDescription attribute is not "
         "defined for assembly {0}.", assyName );
      }

   };

}


/*
 * Output:
 * The AssemblyDescription attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy, 
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr = 
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".", 
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");            
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

Commenti

Nota

A partire da .NET Framework versione 2.0, questo metodo restituisce true se l'assembly contiene gli attributi di sicurezza archiviati nel nuovo formato dei metadati.Starting with the .NET Framework version 2.0, this method returns true if the assembly has security attributes stored in the new metadata format. Gli assembly compilati con la versione 2.0 o versioni successive usano il nuovo formato.Assemblies compiled with version 2.0 or later use the new format. Assembly dinamici e gli assembly compilati con le versioni precedenti di .NET Framework usano il vecchio formato XML.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. Visualizzare creazione di attributi di sicurezza dichiarativa.See Emitting Declarative Security Attributes.

IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type) IsDefined(MemberInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo.Determines whether any custom attributes are applied to a member of a type. I parametri consentono di specificare il membro e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the member, and the type of the custom attribute to search for.

public:
 static bool IsDefined(System::Reflection::MemberInfo ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType);
static member IsDefined : System.Reflection.MemberInfo * Type -> bool
Public Shared Function IsDefined (element As MemberInfo, attributeType As Type) As Boolean

Parametri

element
MemberInfo MemberInfo MemberInfo MemberInfo

Un oggetto derivato dalla classe MemberInfo che descrive un costruttore, un evento, un campo, un metodo, un tipo o un membro di proprietà di una classe.An object derived from the MemberInfo class that describes a constructor, event, field, method, type, or property member of a class.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

element non è un costruttore, un metodo, una proprietà, un evento, un tipo o un campo.element is not a constructor, method, property, event, type, or field.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un MemberInfo come parametro.The following code example illustrates the use of IsDefined, taking a MemberInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef4CS
{
   public ref class TestClass
   {
   public:

      // Assign the Obsolete attribute to a method.

      [Obsolete("This method is obsolete. Use Method2 instead.")]
      void Method1(){}

      void Method2(){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // See if the Obsolete attribute is defined for this method.
         bool isDef = Attribute::IsDefined( mInfo, ObsoleteAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Obsolete Attribute {0} defined for {1} of class {2}.", isDef ? (String^)"is" : "is not", mInfo->Name, clsType->Name );
         
         // If it's defined, display the attribute's message.
         if ( isDef )
         {
            ObsoleteAttribute^ obsAttr = dynamic_cast<ObsoleteAttribute^>(Attribute::GetCustomAttribute( mInfo, ObsoleteAttribute::typeid ));
            if ( obsAttr != nullptr )
                        Console::WriteLine( "The message is: \"{0}\".", obsAttr->Message );
            else
                        Console::WriteLine( "The message could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
using System;
using System.Reflection;

namespace IsDef4CS 
{
    public class TestClass 
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    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");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef) 
            {
                ObsoleteAttribute obsAttr = 
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute( 
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */
Imports System
Imports System.Reflection

Module DemoModule

    Public Class TestClass
        ' Assign the Obsolete attribute to a method.
        <Obsolete("This method is obsolete. Use Method2() instead.")> _
        Public Sub Method1()
        End Sub

        Public Sub Method2()
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' See if the Obsolete attribute is defined for this method.
        Dim isDef As Boolean = Attribute.IsDefined(mInfo, _
            GetType(ObsoleteAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the results.
        Console.WriteLine("The Obsolete attribute {0} defined for " & _
            "method {1} of class {2}.", strDef, mInfo.Name, clsType.Name)
        ' If it's defined, display the attribute's message.
        If isDef = True Then
            Dim attr As Attribute = Attribute.GetCustomAttribute(mInfo, _
                GetType(ObsoleteAttribute))
            If Not attr Is Nothing And TypeOf attr Is ObsoleteAttribute Then
                Dim obsAttr As ObsoleteAttribute = _
                    CType(attr, ObsoleteAttribute)
                Console.WriteLine("The message is: ""{0}""", obsAttr.Message)
            Else
                Console.WriteLine("The message could not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The Obsolete attribute is defined for method Method1 of class TestClass.
' The message is: "This method is obsolete. Use Method2() instead."

Commenti

I predecessori di element vengono cercati gli attributi personalizzati.The ancestors of element are searched for custom attributes.

Nota

A partire da .NET Framework versione 2.0, questo metodo restituisce true se un tipo, metodo o costruttore ha gli attributi di sicurezza archiviati nel nuovo formato dei metadati.Starting with 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 o versioni successive usano il nuovo formato.Assemblies compiled with version 2.0 or later use the new format. Assembly dinamici e gli assembly compilati con le versioni precedenti di .NET Framework usano il vecchio formato XML.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. Visualizzare creazione di attributi di sicurezza dichiarativa.See Emitting Declarative Security Attributes.

IsDefined(Module, Type) IsDefined(Module, Type) IsDefined(Module, Type) IsDefined(Module, Type)

Determina se vengono applicati attributi personalizzati del tipo specificato a un modulo.Determines whether any custom attributes of a specified type are applied to a module. I parametri consentono di specificare il modulo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the module, and the type of the custom attribute to search for.

public:
 static bool IsDefined(System::Reflection::Module ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.Module element, Type attributeType);
static member IsDefined : System.Reflection.Module * Type -> bool
Public Shared Function IsDefined (element As Module, attributeType As Type) As Boolean

Parametri

element
Module Module Module Module

Oggetto derivato dalla classe Module che descrive un file eseguibile portabile.An object derived from the Module class that describes a portable executable file.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un Module come parametro.The following code example illustrates the use of IsDefined, taking a Module as a parameter.

using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // See if the Debuggable attribute is defined for this module.
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );
         
         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} "
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );
         
         // If the attribute is defined, display the JIT settings.
         if ( isDef )
         {
            
            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               Console::WriteLine( "JITTrackingEnabled is {0}.", dbgAttr->IsJITTrackingEnabled );
               Console::WriteLine( "JITOptimizerDisabled is {0}.", dbgAttr->IsJITOptimizerDisabled );
            }
            else
                        Console::WriteLine( "The Debuggable attribute "
            "could not be retrieved." );
         }
      }

   };

}


/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module, 
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module, 
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */
Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata.
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined.
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String
        If isDef = True Then
            strDef = "is"
        Else
            strDef = "is not"
        End If
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings.
        If isDef = True Then
            ' Retrieve the attribute itself.
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If
        End If
    End Sub
End Module

' Output:
' The debuggable attribute is defined for module DemoModule.
' JITTrackingEnabled is True.
' JITOptimizerDisabled is False.

Commenti

I predecessori di element non vengono cercati gli attributi personalizzati.The ancestors of element are not searched for custom attributes.

IsDefined(Assembly, Type) IsDefined(Assembly, Type) IsDefined(Assembly, Type) IsDefined(Assembly, Type)

Stabilisce se vengono applicati attributi personalizzati a un assembly.Determines whether any custom attributes are applied to an assembly. I parametri consentono di specificare l'assembly e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the assembly, and the type of the custom attribute to search for.

public:
 static bool IsDefined(System::Reflection::Assembly ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType);
static member IsDefined : System.Reflection.Assembly * Type -> bool
Public Shared Function IsDefined (element As Assembly, attributeType As Type) As Boolean

Parametri

element
Assembly Assembly Assembly Assembly

Oggetto derivato dalla classe Assembly che descrive una raccolta riutilizzabile di moduli.An object derived from the Assembly class that describes a reusable collection of modules.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un Assembly come parametro.The following code example illustrates the use of IsDefined, taking an Assembly as a parameter.

using namespace System;
using namespace System::Reflection;

// Add an AssemblyDescription attribute
[assembly:AssemblyDescription("A sample description")];
namespace IsDef1CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;
         
         // Get the assembly object.
         Assembly^ assy = clsType->Assembly;
         
         // Store the assembly's name.
         String^ assyName = assy->GetName()->Name;
         
         //Type assyType = assy.GetType();
         // See if the Assembly Description is defined.
         bool isdef = Attribute::IsDefined( assy, AssemblyDescriptionAttribute::typeid );
         if ( isdef )
         {
            
            // Affirm that the attribute is defined.
            Console::WriteLine( "The AssemblyDescription attribute "
            "is defined for assembly {0}.", assyName );
            
            // Get the description attribute itself.
            AssemblyDescriptionAttribute^ adAttr = dynamic_cast<AssemblyDescriptionAttribute^>(Attribute::GetCustomAttribute( assy, AssemblyDescriptionAttribute::typeid ));
            
            // Display the description.
            if ( adAttr != nullptr )
                        Console::WriteLine( "The description is \"{0}\".", adAttr->Description );
            else
                        Console::WriteLine( "The description could not "
            "be retrieved." );
         }
         else
                  Console::WriteLine( "The AssemblyDescription attribute is not "
         "defined for assembly {0}.", assyName );
      }

   };

}


/*
 * Output:
 * The AssemblyDescription attributeis defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy, 
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr = 
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".", 
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");            
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Add an AssemblyDescription attribute.
<Assembly: AssemblyDescription("A sample description")> 

Module DemoModule
    Sub Main()
        ' Get the assembly for this module.
        Dim assy As System.Reflection.Assembly = GetType(DemoModule).Assembly
        ' Store the assembly name.
        Dim assyName As String = assy.GetName().Name
        ' See if the AssemblyDescription attribute is defined.
        If Attribute.IsDefined(assy, GetType(AssemblyDescriptionAttribute)) _
            Then
            ' Affirm that the attribute is defined. Assume the filename of
            ' this code example is "IsDef1VB".
            Console.WriteLine("The AssemblyDescription attribute is " & _
                "defined for assembly {0}.", assyName)
            ' Get the description attribute itself.
            Dim attr As Attribute = Attribute.GetCustomAttribute( _
                assy, GetType(AssemblyDescriptionAttribute))
            ' Display the description.
            If Not attr Is Nothing And _
                TypeOf attr Is AssemblyDescriptionAttribute Then
                Dim adAttr As AssemblyDescriptionAttribute = _
                    CType(attr, AssemblyDescriptionAttribute)
                Console.WriteLine("The description is " & _
                    Chr(34) & "{0}" & Chr(34) & ".", adAttr.Description)
            Else
                Console.WriteLine("The description could not be retrieved.")
            End If
        Else
            Console.WriteLine("The AssemblyDescription attribute is not " & _
                              "defined for assembly {0}.", assyName)
        End If
    End Sub
End Module

' Output:
' The AssemblyDescription attribute is defined for assembly IsDef1VB.
' The description is "A sample description".

Commenti

Nota

A partire da .NET Framework versione 2.0, questo metodo restituisce true se l'assembly contiene gli attributi di sicurezza archiviati nel nuovo formato dei metadati.Starting with the .NET Framework version 2.0, this method returns true if the assembly has security attributes stored in the new metadata format. Gli assembly compilati con la versione 2.0 o versioni successive usano il nuovo formato.Assemblies compiled with version 2.0 or later use the new format. Assembly dinamici e gli assembly compilati con le versioni precedenti di .NET Framework usano il vecchio formato XML.Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. Visualizzare creazione di attributi di sicurezza dichiarativa.See Emitting Declarative Security Attributes.

IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type) IsDefined(ParameterInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo.Determines whether any custom attributes are applied to a method parameter. I parametri consentono di specificare il parametro del metodo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.Parameters specify the method parameter, and the type of the custom attribute to search for.

public:
 static bool IsDefined(System::Reflection::ParameterInfo ^ element, Type ^ attributeType);
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType);
static member IsDefined : System.Reflection.ParameterInfo * Type -> bool
Public Shared Function IsDefined (element As ParameterInfo, attributeType As Type) As Boolean

Parametri

element
ParameterInfo ParameterInfo ParameterInfo ParameterInfo

Un oggetto derivato dalla classe ParameterInfo che descrive un parametro di un membro di una classe.An object derived from the ParameterInfo class that describes a parameter of a member of a class.

attributeType
Type Type Type Type

Tipo o tipo base dell'attributo personalizzato da cercare.The type, or a base type, of the custom attribute to search for.

Restituisce

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.true if a custom attribute of type attributeType is applied to element; otherwise, false.

Eccezioni

element o attributeType è null.element or attributeType is null.

attributeType non deriva da Attribute.attributeType is not derived from Attribute.

Esempi

Esempio di codice seguente viene illustrato l'utilizzo di IsDefined, accettando un ParameterInfo come parametro.The following code example illustrates the use of IsDefined, taking a ParameterInfo as a parameter.

using namespace System;
using namespace System::Reflection;

namespace IsDef5CS
{
   public ref class TestClass
   {
   public:

      // Assign a ParamArray attribute to the parameter using the keyword.
      void Method1(... array<String^>^args ){}

   };

   ref class DemoClass
   {
   public:
      static void Main()
      {
         
         // Get the class type to access its metadata.
         Type^ clsType = TestClass::typeid;
         
         // Get the MethodInfo object for Method1.
         MethodInfo^ mInfo = clsType->GetMethod( "Method1" );
         
         // Get the ParameterInfo array for the method parameters.
         array<ParameterInfo^>^pInfo = mInfo->GetParameters();
         if ( pInfo != nullptr )
         {
            
            // See if the ParamArray attribute is defined.
            bool isDef = Attribute::IsDefined( pInfo[ 0 ], ParamArrayAttribute::typeid );
            
            // Display the result.
            Console::WriteLine( "The ParamArray attribute {0} defined for "
            "parameter {1} of method {2}.", isDef ? (String^)"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.
 */
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.
 */
Imports System
Imports System.Reflection

Module DemoModule
    Public Class TestClass
        ' Assign a ParamArray attribute to the parameter using the keyword.
        Public Sub Method1(ByVal ParamArray args As String())
        End Sub
    End Class

    Sub Main()
        ' Get the class type to access its metadata.
        Dim clsType As Type = GetType(TestClass)
        ' Get the MethodInfo object for Method1.
        Dim mInfo As MethodInfo = clsType.GetMethod("Method1")
        ' Get the ParameterInfo array for the method parameters.
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        If Not pInfo(0) Is Nothing Then
            ' See if the ParamArray attribute is defined.
            Dim isDef As Boolean = Attribute.IsDefined(pInfo(0), _
                                   GetType(ParamArrayAttribute))
            Dim strDef As String
            If isDef = True Then
                strDef = "is"
            Else
                strDef = "is not"
            End If
            ' Display the result.
            Console.WriteLine("The ParamArray attribute {0} defined " & _
                              "for parameter {1} of method {2}.", _
                              strDef, pInfo(0).Name, mInfo.Name)
        Else
            Console.WriteLine("Could not retrieve parameter information " & _
                              "for method {0}.", mInfo.Name)
        End If
    End Sub
End Module

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

Commenti

I predecessori di element vengono cercati gli attributi personalizzati.The ancestors of element are searched for custom attributes.

Si applica a