Module.IsDefined(Type, Boolean) Metodo

Definizione

Restituisce un valore che indica se il tipo di attributo specificato è stato applicato a questo modulo.

C#
public virtual bool IsDefined (Type attributeType, bool inherit);

Parametri

attributeType
Type

Tipo di attributo personalizzato per cui effettuare il test.

inherit
Boolean

Questo argomento viene ignorato per gli oggetti di questo tipo.

Restituisce

true se una o più istanze di attributeType sono state applicate al modulo; in caso contrario false.

Implementazioni

Eccezioni

attributeType è null.

attributeType non è un oggetto Type fornito dal runtime. Ad esempio, attributeType è un oggetto TypeBuilder.

Esempio

Nell'esempio IsDefined seguente viene illustrato un uso del metodo .

C#
using System;
using System.Reflection;

//Define a module-level attribute.
[module: ReflectionModule_Examples.MySimpleAttribute("module-level")]

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
            Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, false));
        }
    }

    //A very simple custom attribute.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
    public class MySimpleAttribute : Attribute
    {
        private string name;

        public MySimpleAttribute(string newName)
        {
            name = newName;
        }
    }
}

Si applica a

Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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, 4.8.1
.NET Standard 2.0, 2.1