Leggere in inglese

Condividi tramite


Module.GetCustomAttributes Metodo

Definizione

Restituisce attributi personalizzati.

Overload

GetCustomAttributes(Boolean)

Restituisce tutti gli attributi personalizzati.

GetCustomAttributes(Type, Boolean)

Ottiene gli attributi personalizzati del tipo specificato.

GetCustomAttributes(Boolean)

Origine:
Module.cs
Origine:
Module.cs
Origine:
Module.cs

Restituisce tutti gli attributi personalizzati.

public virtual object[] GetCustomAttributes (bool inherit);

Parametri

inherit
Boolean

Questo argomento viene ignorato per gli oggetti di questo tipo.

Restituisce

Object[]

Matrice di tipo Object contenente tutti gli attributi personalizzati.

Implementazioni

Esempio

Nell'esempio seguente vengono visualizzati i nomi dei moduli corrispondenti ai criteri di ricerca specificati.

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];
            object[] attributes;
            attributes = myModule.GetCustomAttributes(true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}.", o.ToString());
            }
        }
    }
    //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

.NET 9 e altre versioni
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

GetCustomAttributes(Type, Boolean)

Origine:
Module.cs
Origine:
Module.cs
Origine:
Module.cs

Ottiene gli attributi personalizzati del tipo specificato.

public virtual object[] GetCustomAttributes (Type attributeType, bool inherit);

Parametri

attributeType
Type

Tipo di attributo da ottenere.

inherit
Boolean

Questo argomento viene ignorato per gli oggetti di questo tipo.

Restituisce

Object[]

Matrice di tipo Object contenente tutti gli attributi personalizzati del tipo specificato.

Implementazioni

Eccezioni

attributeType è null.

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

Esempio

Nell'esempio seguente vengono visualizzati i nomi dei moduli del tipo specificato che corrispondono ai criteri di ricerca specificati.

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];
            object[] attributes;
            //Get only MySimpleAttribute attributes for this module.
            attributes = myModule.GetCustomAttributes(
                myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", false, false),
                true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}", o.ToString());
            }
        }
    }

    // Define 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

.NET 9 e altre versioni
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