2012-07-06 16 views
15

मैं C# में कॉलिंग विधि का पूरा नाम कैसे ढूंढ सकता हूं। मैं समाधान देखा है:कॉलिंग विधि का पूरा नाम कैसे खोजें 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

लेकिन वे केवल मुझे शीर्ष स्तर दे। उदाहरण पर विचार करें:

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

यह केवल आउटपुट 'मुख्य' मैं इसे कैसे मुद्रित करने के लिए 'Sandbox.Program.Main' मिल सकता है?

कोई भी पूछने से पहले कि मुझे इसका उपयोग क्यों करना है, यह एक साधारण लॉगिंग फ्रेमवर्क के लिए है जिस पर मैं काम कर रहा हूं।

संपादित

पर Matzi के जवाब जोड़ना:

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

उत्पादन 'Sandbox.Program.Main' की तरह यह

+1

के संभावित डुप्लिकेट [प्राप्त करने System.Reflection का उपयोग करते हुए एक विधि का पूरा नाम] (http://stackoverflow.com/questions/2968352/using-system-reflection-to-get-a-methods-full -नाम) – user7116

उत्तर

27

यह कुछ है चाहिए:

यहाँ समाधान है here की तरह।

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

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

धन्यवाद! मैंने नाम को संपादित करने के लिए सिर्फ यह पता लगाया कि मैंने नेमस्पेस को कैसे ढूंढ लिया। –

+2

काम नहीं करता है। विधि कॉलिंग विधि नहीं हो सकती है लेकिन बाहर के लोगों में से एक - कॉलिंग विधि अब मौजूद नहीं हो सकती है, यानी रेखांकित किया गया है। – TomTom

+0

क्या नेस्टेड कक्षाएं भी प्राप्त करना संभव है? यदि मैंने प्रोग्राम के अंदर एक कक्षा बनाई है, तो इसे 'Foo' कहें, और मेरे पास 'Foo' में एक विधि है (इसे 'बार' कहें) कॉल 'Program.test()' इसे 'Sandbox.Program.Foo' प्रिंट करना चाहिए .बार –

0

System.Reflection.MethodBase विधि GetCurrentMethod आप कक्षाओं आदि का उपयोग callstack के बारे में पूरी जानकारी प्राप्त कर सकते में

+0

कैसे? ['GetCurrentMethod'] (https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod (v = vs.110) .aspx) एक [' MethodBase'] देता है (https://msdn.microsoft.com/en-us/library/system.reflection.methodbase(v=vs.110).aspx) जो स्टैक ट्रेस (वर्तमान प्रकार को छोड़कर) के बारे में कुछ भी प्रकट नहीं करता है। – c24w

4

मुझे लगता है कि सबसे अच्छा तरीका है पूरा नाम पाने के लिए है:

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

या इस

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

इस विधि के साथ आप विश्वसनीय रूप से पूर्ण हो सकते हैं नाम

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}()"); 
     } 
    } 
संबंधित मुद्दे