Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Method/Function name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pz - 29 Mar 2008 05:53 GMT
How do I output the name of the method or a function without hard-coding it
in the application?
Say, I want application to write to stdout the name of the method that
currently is getting executed, to give me better idea how the program logic
is flowing (I am writing a multithreaded application). Instead of copying
Console.Writeline("Method123");
and pasting it to 100+ methods, and then correcting the names of the modules
in each one of them, is there a variable that would contain the name of the
method that has control. There's got to be a better way than hard-coding
it...
DrewCE - 29 Mar 2008 07:11 GMT
If you don't care about performance, you could use the StackTrace and
StackFrame objects.  To get the name of the current method:

StackTrace st = new StackTrace();
string methodName = st.GetFrame(0).GetMethod().Name;

..or something like that.  A quick google yielded the following....

http://www.dotnetjunkies.com/WebLog/mames/archive/2004/08/03/21091.aspx

..you can even get the line number...

http://msdn2.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

-Drew

> How do I output the name of the method or a function without hard-coding
> it in the application?
[quoted text clipped - 7 lines]
> name of the method that has control. There's got to be a better way than
> hard-coding it...
vvnraman - 29 Mar 2008 07:38 GMT
> How do I output the name of the method or a function without hard-coding it
> in the application?
[quoted text clipped - 6 lines]
> method that has control. There's got to be a better way than hard-coding
> it...

Hi pz
Its very simple to get information about the currently executing
method.
You simply have to call the static GetCurrentMethod() of the
MethodBase class.
It will return a MethodBase object from where you can get all the
information
you need about the method.

Use it inside a method as :

System.Reflection.MethodBase currMethod =
System.Reflection.MethodBase.GetCurrentMethod();

In your case you only want the name of the method so you can output it
as

Console.Writeline(currMethod.Name);

Hope this helps
Prateek Raman
Bob Powell [MVP] - 29 Mar 2008 14:21 GMT
You might be better off using a profiling tool to see this sort of
information.

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> How do I output the name of the method or a function without hard-coding
> it in the application?
[quoted text clipped - 7 lines]
> name of the method that has control. There's got to be a better way than
> hard-coding it...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.