.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Get the name of a calling sub or function

App: VB.NET

Scenario:
(i) I have a sub
public sub foo(i as integer)
  ' stuff goes in here
end sub

(ii)
Two Subs call  foo()

public sub Alpha()
  ...
  foo(10);
end sub

Public sub Beta()
  ...
  foo(15);
end sub

Question:
Inside foo(), i'd like to print the name of the sub that called it. The output would be along the lines of
" Foo called by ... Alpha()"

Is this possible?

I was hoping System.Reflection.MethodBase.GetCurrentMethod.Name() would help but it won't return the names Alpha or Beta.


Thanks
RJ
R.James Send private email
Sunday, June 24, 2007
 
 
Dim stackFrame As New Diagnostics.StackFrame(1)
stackFrame.GetMethod.DeclaringType.FullName

This works by getting access to the call stack. The parameter
of 1 means look at the frame one above the current stack frame. The GetMethod function returns the method within which the specified stack frame is executing. Finally, the DeclaringType property returns the actual type that declared
the method, as opposed to the base or derived type.
Mark Pearce Send private email
Sunday, June 24, 2007
 
 
Why would you want to get the name of a calling sub or function????

I'm sure it's possible to do in .NET, but just say no...

This does not sound like it's good programming practice. The sub/function should not meddle with  or know about anything outside of it.

Does the function do different things depending on the calling function? If so, then just add a parameter (with a name that makes sense, not the caller's name).
Totally Agreeing
Sunday, June 24, 2007
 
 
> I'm sure it's possible to do in .NET, but just say no...
... and may not be reliable because of inlining done by the JITter.
Joe
Monday, June 25, 2007
 
 
>> .. and may not be reliable because of inlining done by the JITter. <<

To tell the JIT compiler not to inline a specific method, just add an attribute to that method. I can't recall the attribute name, but Mr Google will know.
Mark Pearce Send private email
Monday, June 25, 2007
 
 
>> The sub/function should not meddle with  or know about anything outside of it. <<

In the OP's example, there's no "meddling", just printing. This can be very useful during debugging, for example to understand ambiguous overloads.
Mark Pearce Send private email
Monday, June 25, 2007
 
 
Mark,
Thanks for the reply. It actually worked! I just made a small change as follows

_Name = stackFrame.GetMethod.Name.tostring()
_Name = _Name & stackFrame.GetMethod.DeclaringType.FullName.tostring()

So I can now get the full info as in
WinApp.Form1.Alpha(), or
WinApp.Form.Alpha.Beta(), etc.

And you're right about the "meddling" bit too! I'm new to VB.NET & I was trying to port an old vb6 logger project to .NET with this feature.

Thanks
RJ
R.James Send private email
Monday, June 25, 2007
 
 
There's some whacky stuff that goes on with inlining, and using the stack frame. Check out Rocky Lhotka's clsa.net (http://www.lhotka.net/Area.aspx?id=4) to see how he handles it. He uses the stack frame to check security, in the case of clsa.net.
PeterL Send private email
Monday, June 25, 2007
 
 
==>Why would you want to get the name of a calling sub or function????

I do it all the time for debugging, logging, and profiling. Quite handy, although largely of no use for production use of the code -- mostly a "to assist in development" usage.
Sgt.Sausage
Monday, June 25, 2007
 
 
> To tell the JIT compiler not to inline a specific method, just add an attribute to that method.

The benefits of knowing the name of the calling method would need to be very compelling for me to want to do that.  It's the sort of thing that maintenance programmers down the line will easily forget when adding methods / refactoring etc.  I don't think I'd do it for the kind of thing the OP is talking about.
Joe
Monday, June 25, 2007
 
 
>> Check out Rocky Lhotka's clsa.net to see how he handles it. <<

Looks like Rocky handles it the way I suggested, with the MethodImplOptions.NoInlining attribute:
http://www.lhotka.net/Article.aspx?area=4&id=0708c745-f009-4d09-9f91-6a349b5b0317
Mark Pearce Send private email
Monday, June 25, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz