Lire en anglais

Partager via


MethodBase.GetCurrentMethod Méthode

Définition

Retourne un MethodBase objet représentant la méthode en cours d’exécution.

C#
public static System.Reflection.MethodBase? GetCurrentMethod();
C#
public static System.Reflection.MethodBase GetCurrentMethod();

Retours

GetCurrentMethod() est une méthode statique appelée à partir d’une méthode en cours d’exécution et qui retourne des informations sur cette méthode.

Objet MethodBase représentant la méthode en cours d’exécution.

Exceptions

Ce membre a été appelé avec un mécanisme de liaison tardive.

Exemples

L’exemple suivant définit deux types. La première est une classe non générique, TestClass, inclut un constructeur, une méthode nommée GetValueet une propriété en lecture-écriture nommée GetValue. La seconde est une classe générique nommée TestClass<T> qui inclut un constructeur, une GetValue méthode et une méthode générique, ConvertValue<Y>. Chaque constructeur, méthode et accesseur de propriété inclut un appel à la GetCurrentMethod méthode .

C#
using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      var t = new TestClass();  
      Console.WriteLine(t.GetValue());
      t.Value = 10;
      Console.WriteLine(t.Value);
      Console.WriteLine();
      
      var tg =new Test<int>(200);
      Console.WriteLine(tg.GetValue());
      var b = tg.ConvertValue<Byte>();
      Console.WriteLine("{0} -> {1} ({2})", tg.GetValue().GetType().Name,
                        b, b.GetType().Name);
   }
}        

public class TestClass
{
   private Nullable<int> _value;
   
   public TestClass()
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
   }
   
   public TestClass(int value)
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
      _value = value;
   }
   
   public int Value
   {  
      get {
         MethodBase m = MethodBase.GetCurrentMethod();
         Console.WriteLine("Executing {0}.{1}", 
                           m.ReflectedType.Name, m.Name);
         return _value.GetValueOrDefault();
      }
      set {
         MethodBase m = MethodBase.GetCurrentMethod();
         Console.WriteLine("Executing {0}.{1}", 
                           m.ReflectedType.Name, m.Name);
         _value = value;
      }
   }
   
   public int GetValue()
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
      return this.Value;
   }
}

public class Test<T>
{
   private T value;
   
   public Test(T value)
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
      this.value = value;
   }
   
   public T GetValue()
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
      return value;
   }
   
   public Y ConvertValue<Y>() 
   {
      MethodBase m = MethodBase.GetCurrentMethod();
      Console.WriteLine("Executing {0}.{1}", 
                        m.ReflectedType.Name, m.Name);
      Console.Write("      Generic method: {0}, definition: {1}, Args: ", 
                        m.IsGenericMethod, m.IsGenericMethodDefinition);
      if (m.IsGenericMethod) {
         foreach (var arg in m.GetGenericArguments())
            Console.Write("{0} ", arg.Name);
      }
      Console.WriteLine();
      try {
         return (Y) Convert.ChangeType(value, typeof(Y));
      }
      catch (OverflowException) {
         throw; 
      }   
      catch (InvalidCastException) {
         throw;
      }   
   }   
}
// The example displays the following output:
//       Executing TestClass..ctor
//       Executing TestClass.GetValue
//       Executing TestClass.get_Value
//       0
//       Executing TestClass.set_Value
//       Executing TestClass.get_Value
//       10
//       
//       Executing Test`1..ctor
//       Executing Test`1.GetValue
//       200
//       Executing Test`1.ConvertValue
//             Generic method: True, definition: True, Args: Y
//       Executing Test`1.GetValue
//       Int32 -> 200 (Byte)

Remarques

Si la méthode en cours d’exécution est définie sur un type générique, le MethodInfo retourné par GetCurrentMethod est obtenu à partir de la définition de type générique (autrement dit, MethodBase.ContainsGenericParameters retourne true). Par conséquent, elle ne reflète pas les arguments de type qui ont été utilisés lors de l’appel de la méthode. Par exemple, si une méthode M() est définie sur un type C<T> générique (C(Of T) en Visual Basic) et GetCurrentMethod est appelée à partir de C<string>.M(), retourne GetCurrentMethodC<T>.M() (C(Of T).M() en Visual Basic).

Si la méthode en cours d’exécution est une méthode générique, GetCurrentMethod retourne la définition de méthode générique. Si la méthode générique est définie sur un type générique, le MethodInfo est obtenu à partir de la définition de type générique.

S’applique à

Produit Versions
.NET 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 2.0, 2.1