Getting name of caller

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'm implementing a tracing class with a WriteLine method, and would like to
record the name of the function which is calling WriteLine. Is there a way
to do this?
 
Dan,

In this case, you would want to create an instance of the StackFrame
class, passing 1 to the constructor indicating that you want to get the
stack frame for the one above the currently executing code.

Once there, you can get the MethodInfo instance for that method, and get
the name of the method, as well as the type.

However, this can have a hit on performance (walking the stack usually
does).

Hope this helps.
 
Thanks Nicholas.

Nicholas Paldino said:
Dan,

In this case, you would want to create an instance of the StackFrame
class, passing 1 to the constructor indicating that you want to get the
stack frame for the one above the currently executing code.

Once there, you can get the MethodInfo instance for that method, and get
the name of the method, as well as the type.

However, this can have a hit on performance (walking the stack usually
does).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dan said:
I'm implementing a tracing class with a WriteLine method, and would like
to
record the name of the function which is calling WriteLine. Is there a
way
to do this?
 

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