Ler en inglés

Compartir por


Module.IsDefined(Type, Boolean) Método

Definición

Devuelve un valor que indica si el tipo de atributo especificado se ha aplicado a este módulo.

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

Parámetros

attributeType
Type

Tipo de atributo personalizado que se va a probar.

inherit
Boolean

Se omite este argumento para los objetos de este tipo.

Devoluciones

Es true si se han aplicado una o varias instancias de attributeType a este módulo; de lo contrario, es false.

Implementaciones

Excepciones

attributeType es null.

attributeType no es un objeto Type proporcionado por el tiempo de ejecución. Por ejemplo, attributeType es un objeto TypeBuilder.

Ejemplos

En el ejemplo siguiente se muestra un uso del IsDefined método .

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;
        }
    }
}

Se aplica a

Produto Versións
.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