Get name of current method in C# | Techie Delight

文章推薦指數: 80 %
投票人數:10人

Get name of current method in C# · 1. Using StackTrace Class ( System.Diagnostics ) · 2. Using MethodBase.GetCurrentMethod() method. Skiptocontent ThispostwilldiscusshowtogetthenameofthecurrentmethodbeingexecutedinC#. 1.UsingStackTraceClass(System.Diagnostics) Astacktraceisanorderedcollectionofoneormorestackframesthatstoredebugginganddiagnosticinformation. ThefollowingexamplecreatesasimplestacktraceusingtheStackTraceinstance.Thesecondframeofthestacktracecontainsthestackframeofthecurrentmethod. 1234567891011121314151617181920212223 usingSystem;usingSystem.Diagnostics; publicclassExample{    publicstaticstringGetCurrentMethodName()    {        StackTracestackTrace=newStackTrace();        StackFramestackFrame=stackTrace.GetFrame(1);         returnstackFrame.GetMethod().Name;    }     publicstaticvoidMain()    {        stringcurrentMethod=GetCurrentMethodName();        Console.WriteLine(currentMethod);    }} /*    Output:Main*/ Download  RunCode   Ifyoudon’twanttocreateahelpermethod,changethecodetothefollowing.Herethefirstframerepresentsthetopofthestack,whichisthelastmethodinvocationinthesequence. 123456789101112131415161718 usingSystem;usingSystem.Diagnostics; publicclassExample{    publicstaticvoidMain()    {        StackTracestackTrace=newStackTrace();        StackFramestackFrame=stackTrace.GetFrame(0);         stringcurrentMethod=stackFrame.GetMethod().Name;        Console.WriteLine(currentMethod);    }} /*    Output:Main*/ Download  RunCode Or 12345678910111213141516 usingSystem;usingSystem.Diagnostics; publicclassExample{    publicstaticvoidMain()    {        StackFramestackFrame=newStackFrame(0);        stringcurrentMethod=stackFrame.GetMethod().Name;        Console.WriteLine(currentMethod);    }} /*    Output:Main*/ Download  RunCode 2.UsingMethodBase.GetCurrentMethod()method Alternatively,theMethodBase.GetCurrentMethod()methodcanbeused,whichreturnsaMethodBaseobjectrepresentingthecurrentlyexecutingmethod. 12345678910111213141516 usingSystem;usingSystem.Diagnostics;usingSystem.Reflection; publicclassExample{    publicstaticvoidMain()    {        stringcurrentMethod=MethodBase.GetCurrentMethod().Name;        Console.WriteLine(currentMethod);    }} /*    Output:Main*/ Download  RunCode That’sallaboutgettingthenameofthecurrentmethodinC#. RatethispostSubmitRatingAveragerating4.82/5.Votecount:22Novotessofar!Bethefirsttoratethispost.Wearesorrythatthispostwasnotusefulforyou!Tellushowwecanimprovethispost?SubmitFeedback Thanksforreading. PleaseuseouronlinecompilertopostcodeincommentsusingC,C++,Java,Python,JavaScript,C#,PHP,andmanymorepopularprogramminglanguages. Likeus?Referustoyourfriendsandhelpusgrow.Happycoding🙂 Subscribe Notifyof newfollow-upcomments newrepliestomycomments Name* Email* Name* Email* 0Comments InlineFeedbacks Viewallcomments LoadMoreComments BrowseAlgorithm Amazon Beginner BinarySearch BitHacks Bottom-up Breadth-firstsearch Depth-firstsearch Easy FIFO Greedy Hard Hashing LIFO Maze Medium Microsoft MustKnow PriorityQueue Recursive SlidingWindow Top-down Trie Subscribetonewposts Enteryouremailaddresstosubscribetonewposts. EmailAddress Subscribe Thiswebsiteusescookies.Byusingthissiteyouagreetotheuseofcookies,ourpolicies,copyrighttermsandotherconditions.Readour PrivacyPolicy. DoNOTfollowthislinkoryouwillbebannedfromthesite! Insert



請為這篇文章評分?