2012-07-06 30 views
15

Come posso trovare il nome completo di un metodo di chiamata in C#. Ho visto soluzioni:Come trovare il metodo FULL del metodo di chiamata C#

How I can get the calling methods in C#

How can I find the method that called the current method?

Get Calling function name from Called function

Ma solo mi indica il livello superiore. Si consideri l'esempio:

namespace Sandbox 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      test(); 
     } 

     static void test() 
     { 
      var stackTrace = new StackTrace(); 
      var methodBase = stackTrace.GetFrame(1).GetMethod(); 
      Console.WriteLine(methodBase.Name); 
     } 
    } 
} 

Emette semplicemente 'principale' Come posso ottenere per stampare 'Sandbox.Program.Main'?

Prima che qualcuno inizi a chiedere perché devo usarlo, è per un semplice framework di registrazione su cui sto lavorando.

EDIT

Aggiunta sulla risposta del Matzi:

ecco la soluzione:

namespace Sandbox 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      test(); 
     } 

     static void test() 
     { 
      var stackTrace = new StackTrace(); 
      var methodBase = stackTrace.GetFrame(1).GetMethod(); 
      var Class = methodBase.ReflectedType; 
      var Namespace = Class.Namespace;   //Added finding the namespace 
      Console.WriteLine(Namespace + "." + Class.Name + "." + methodBase.Name); 
     } 
    } 
} 

Produce 'Sandbox.Program.Main' come dovrebbe

+1

possibile duplicato del [using System.Reflection per ottenere un metodo Nome completo] (http://stackoverflow.com/questions/2968352/using-system-reflection-to-get-a-methods-full -name) – user7116

risposta

27

Questo è qualcosa come here.

MethodBase method = stackTrace.GetFrame(1).GetMethod(); 
string methodName = method.Name; 
string className = method.ReflectedType.Name; 

Console.WriteLine(className + "." + methodName); 
+0

Grazie! Ho modificato la domanda solo per aggiungere il modo in cui ho trovato il Namespace. –

+2

Non funziona. Il metodo potrebbe non essere il metodo di chiamata, ma uno di quelli esterni: il metodo di chiamata potrebbe non esistere più, ovvero essere stato allineato. – TomTom

+0

È possibile anche ottenere classi annidate? Se ho creato una classe all'interno di Program, chiamiamolo 'Foo', e ho un metodo in' Foo' (chiamiamolo 'Bar') chiama' Program.test() 'Dovrebbe stampare' Sandbox.Program.Foo .Bar' –

0

in System.Reflection.MethodBase metodo GetCurrentMethod si possono trovare tutte le informazioni sui stack utilizzando le classi ecc

+0

Come? ['GetCurrentMethod'] (https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod (v = vs.110) .aspx) restituisce un [' MethodBase'] (https://msdn.microsoft.com/en-us/library/system.reflection.methodbase(v=vs.110).aspx) che non sembra esporre nulla sulla traccia dello stack (tranne il tipo corrente). – c24w

4

Penso che il modo migliore per ottenere il nome completo è:

this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name; 

o provare questo

string method = string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name); 
0

Con questo metodo è possibile ottenere in modo affidabile il pieno nome

public void HandleException(Exception ex, [CallerMemberName] string caller = "") 
    { 
     if (ex != null) 
     { 
      while (ex.InnerException != null) 
       ex = ex.InnerException; 

      foreach (var method in new StackTrace().GetFrames()) 
      { 
       if (method.GetMethod().Name == caller) 
       { 
        caller = $"{method.GetMethod().ReflectedType.Name}.{caller}"; 
        break; 
       } 
      } 

      Console.WriteLine($"Exception: {ex.Message} Caller: {caller}()"); 
     } 
    } 
Problemi correlati