Get name of current method in C# | Techie Delight
文章推薦指數: 80 %
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
延伸文章資訊
- 1Learn C# Properties: Getters and Setters at ... - Codeasy
Discover the best practices with properties in C Sharp language - C# Properties vs Public fields....
- 2C# Properties (GET, SET) - Tutlane
In c# properties, the get accessor will be invoked while reading the value of a property, and whe...
- 3C# set 與get 用法與範例| 高清水_黑白講 - - 點部落
C# set 與get 用法與範例. 54411; 0. 學習記錄. //get 存取子"提供",將該值(字串)進行判斷、處理再回傳 //set 存取子"設定",用於處理計算及邏輯 ...
- 4get - C# 參考
get 關鍵字會在屬性或索引子中定義「存取子」 get 方法,以傳回屬性值或索引子項目。 如需詳細資訊,請參閱屬性、自動實作的屬性和索引子。
- 5Get name of current method in C# | Techie Delight
Get name of current method in C# · 1. Using StackTrace Class ( System.Diagnostics ) · 2. Using Me...