Method/Function name

  • Thread starter Thread starter pz
  • Start date Start date
P

pz

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...
 
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...

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
 
You might be better off using a profiling tool to see this sort of
information.

--
--
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top