Identifying the Method Name Calling Your Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a function called RegisterXX. Its called from a bunch of places. I
need to know the method name that is calling my RegisterXX

for example given the following code:

public void MyFunctionA
{
//... do something
RegisterXX();
}


public void MyFunctionB
{
//... do something
RegisterXX();
}

I'm trying to hook up some instrumentation code in RegisterXX and want to be
able to pull the caller's name out of the air so I don't have to rely on
developers hooking things up correctly.

public void RegisterXX( EventTypeEnum eventType )
{
string s_MethodName = GetCallingMethodName();
WriteOutInstrumentation( s_MethodName, eventType );
)


so when MyFunctionA calls the RegisterXX method some instrumentation data is
written with the name "MyFunctionA".

I'm trying to be extremely clear here. I just need to know how to find the
calling method.
 
Yes, I could do that. I believe its pretty expensive from a perf
perspective. Isn't there something cheaper?
 
Hi,

Because get the calling method will need walk the call stack, and the
StackTrace is the managed class for the purpose.
Another way is to log the calling method name in a global variable every
you invoke a call, which may somewhat complicated.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
System.Diagnostics.StackFrame s= new System.Diagnostics.StackFrame(1); s.GetMethod().Name;

HTH

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

?
nntp://news.microsoft.com/microsoft.public.dotnet.framework/
Hi,

Here is an example, you may take a look.
StackTrace Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdiagnosticsstacktraceclasstopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.framework]
 
Back
Top