Lire en anglais

Partager via


Module.GetCustomAttributes Méthode

Définition

Retourne les attributs personnalisés.

Surcharges

GetCustomAttributes(Boolean)

Retourne tous les attributs personnalisés.

GetCustomAttributes(Type, Boolean)

Obtient les attributs personnalisés du type spécifié.

GetCustomAttributes(Boolean)

Source:
Module.cs
Source:
Module.cs
Source:
Module.cs

Retourne tous les attributs personnalisés.

C#
public virtual object[] GetCustomAttributes (bool inherit);

Paramètres

inherit
Boolean

Cet argument est ignoré pour les objets de ce type.

Retours

Object[]

Tableau de type Object contenant tous les attributs personnalisés.

Implémente

Exemples

L’exemple suivant affiche les noms de modules qui correspondent aux critères de recherche spécifiés.

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

S’applique à

.NET 9 et autres versions
Produit Versions
.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)

Source:
Module.cs
Source:
Module.cs
Source:
Module.cs

Obtient les attributs personnalisés du type spécifié.

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

Paramètres

attributeType
Type

Type de l'attribut à obtenir.

inherit
Boolean

Cet argument est ignoré pour les objets de ce type.

Retours

Object[]

Tableau de type Object contenant tous les attributs personnalisés du type spécifié.

Implémente

Exceptions

attributeType a la valeur null.

attributeType n’est pas un objet Type fourni par le runtime. Par exemple, attributeType est un objet TypeBuilder.

Exemples

L’exemple suivant affiche les noms de module du type spécifié qui correspondent aux critères de recherche spécifiés.

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

S’applique à

.NET 9 et autres versions
Produit Versions
.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