Attribute.IsDefined Metodo

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.

Overload

IsDefined(ParameterInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo. 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.

IsDefined(Module, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un modulo. I parametri consentono di specificare il modulo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.

IsDefined(MemberInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo. 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.

IsDefined(Assembly, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un assembly. I parametri consentono di specificare l'assembly, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.

IsDefined(MemberInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo. I parametri consentono di specificare il membro e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

IsDefined(Module, Type)

Determina se vengono applicati attributi personalizzati del tipo specificato a un modulo. I parametri consentono di specificare il modulo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

IsDefined(Assembly, Type)

Stabilisce se vengono applicati attributi personalizzati a un assembly. I parametri consentono di specificare l'assembly e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

IsDefined(ParameterInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo. I parametri consentono di specificare il parametro del metodo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

IsDefined(ParameterInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo. 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.

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

Parametri

element
ParameterInfo

Un oggetto derivato dalla classe ParameterInfo che descrive un parametro di un membro di una classe.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

inherit
Boolean

Se è true, specifica di ricercare anche gli attributi personalizzati nei predecessori di element.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

element non è un metodo, un costruttore o un tipo.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un ParameterInfo oggetto come parametro.

C#
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.
 */

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(Module, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un modulo. I parametri consentono di specificare il modulo, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType, bool inherit);

Parametri

element
Module

Oggetto derivato dalla classe Module che descrive un file eseguibile portabile.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

inherit
Boolean

Questo parametro viene ignorato e non influisce sul funzionamento di questo metodo.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un Module oggetto come parametro.

C#
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.
 */

Commenti

Questo metodo ignora il inherit parametro e non esegue ricerche nei predecessori di element per attributi personalizzati.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(MemberInfo, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo. 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.

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType, bool inherit);

Parametri

element
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.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

inherit
Boolean

Se è true, specifica di ricercare anche gli attributi personalizzati nei predecessori di element.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

element non è un costruttore, un metodo, una proprietà, un evento, un tipo o un campo.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un MemberInfo oggetto come parametro.

C#
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.".
 */

Commenti

Nota

A partire dalla .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 di metadati. Gli assembly compilati con la versione 2.0 o successiva usano il nuovo formato. Assembly dinamici e assembly compilati con le versioni precedenti del .NET Framework usano il formato XML precedente. Vedere Emissione di attributi di sicurezza dichiarativi.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(Assembly, Type, Boolean)

Stabilisce se vengono applicati attributi personalizzati a un assembly. I parametri consentono di specificare l'assembly, il tipo dell'attributo personalizzato di cui eseguire la ricerca e un'opzione di ricerca ignorata.

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType, bool inherit);

Parametri

element
Assembly

Oggetto derivato dalla classe Assembly che descrive una raccolta riutilizzabile di moduli.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

inherit
Boolean

Questo parametro viene ignorato e non influisce sul funzionamento di questo metodo.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un Assembly parametro come parametro.

C#
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".
 */

Commenti

Nota

A partire dalla .NET Framework versione 2.0, questo metodo restituisce true se l'assembly ha attributi di sicurezza archiviati nel nuovo formato dei metadati. Gli assembly compilati con la versione 2.0 o successiva usano il nuovo formato. Assembly dinamici e assembly compilati con le versioni precedenti del .NET Framework usano il formato XML precedente. Vedere Emissione di attributi di sicurezza dichiarativi.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(MemberInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al membro di un tipo. I parametri consentono di specificare il membro e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType);

Parametri

element
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.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

element non è un costruttore, un metodo, una proprietà, un evento, un tipo o un campo.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un MemberInfo oggetto come parametro.

C#
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.".
 */

Commenti

I predecessori di element vengono ricercati per attributi personalizzati.

Nota

A partire dalla .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 di metadati. Gli assembly compilati con la versione 2.0 o successiva usano il nuovo formato. Assembly dinamici e assembly compilati con le versioni precedenti del .NET Framework usano il formato XML precedente. Vedere Emissione di attributi di sicurezza dichiarativi.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(Module, Type)

Determina se vengono applicati attributi personalizzati del tipo specificato a un modulo. I parametri consentono di specificare il modulo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType);

Parametri

element
Module

Oggetto derivato dalla classe Module che descrive un file eseguibile portabile.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un Module oggetto come parametro.

C#
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.
 */

Commenti

I predecessori di element non vengono cercati attributi personalizzati.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(Assembly, Type)

Stabilisce se vengono applicati attributi personalizzati a un assembly. I parametri consentono di specificare l'assembly e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType);

Parametri

element
Assembly

Oggetto derivato dalla classe Assembly che descrive una raccolta riutilizzabile di moduli.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un Assembly parametro come parametro.

C#
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".
 */

Commenti

Nota

A partire dalla .NET Framework versione 2.0, questo metodo restituisce true se l'assembly ha attributi di sicurezza archiviati nel nuovo formato dei metadati. Gli assembly compilati con la versione 2.0 o successiva usano il nuovo formato. Assembly dinamici e assembly compilati con le versioni precedenti del .NET Framework usano il formato XML precedente. Vedere Emissione di attributi di sicurezza dichiarativi.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

IsDefined(ParameterInfo, Type)

Stabilisce se vengono applicati attributi personalizzati al parametro di un metodo. I parametri consentono di specificare il parametro del metodo e il tipo dell'attributo personalizzato di cui eseguire la ricerca.

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType);

Parametri

element
ParameterInfo

Un oggetto derivato dalla classe ParameterInfo che descrive un parametro di un membro di una classe.

attributeType
Type

Tipo o tipo base dell'attributo personalizzato da cercare.

Restituisce

Boolean

true se un attributo personalizzato di tipo attributeType è applicato a element; in caso contrario, false.

Eccezioni

element o attributeType è null.

attributeType non deriva da Attribute.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso di IsDefined, prendendo un ParameterInfo oggetto come parametro.

C#
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.
 */

Commenti

I predecessori di element vengono ricercati per attributi personalizzati.

Si applica a

.NET 7 e altre versioni
Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1