Lire en anglais

Partager via


MethodBase.Attributes Propriété

Définition

Obtient les attributs associés à cette méthode.

C#
public abstract System.Reflection.MethodAttributes Attributes { get; }

Valeur de propriété

Une des valeurs de l'objet MethodAttributes.

Implémente

Exemples

L’exemple de code suivant affiche les attributs de la méthode mymethod définie par l’utilisateur.

C#

using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static int Main(string[] args)
    {
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");

        // Get the type.
        Type MyType = Type.GetType("AttributesSample");

        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;

        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }

    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        {
            Console.WriteLine("This type is not an enum.");
            return;
        }

        FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
        for (int i = 0; i < fields.Length; i++)
        {
            int fieldvalue = (int)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

Ce code génère la sortie suivante :

Exemple Reflection.MethodBase.Attributes

Mymethodbase = Void Mymethod(Int32, System.String ByRef, System.String ByRef)

PrivateScope

FamANDAssem

Famille

Public

HideBySig

ReuseSlot

Remarques

Tous les membres ont un ensemble d’attributs, qui sont définis par rapport au type spécifique de membre.

Pour obtenir le MethodAttributes, commencez par obtenir le type . À partir du type , obtenez la méthode . À partir de la méthode , obtenez le MethodAttributes.

Notes pour les responsables de l’implémentation

Utilisez la Attributes propriété pour déterminer si une méthode est public, private, final, virtual, et ainsi de suite.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Voir aussi