Reflection, StackTrace, and Release Builds

B

brianlanning

I'm trying to write a function that can reliably know the name of the
calling method. I've figured out how to do this:

string methodname = (new StackTrace()).GetFrame(1).GetMethod().Name;

This seems to get the name of the method that called the current
method. But there are two problems. One is that I have to hardcode
one step back in the stack. When switching to a release build, this
may not work corerctly because of optimization. The other, more
serious problem is that the method name is stripped from release
buildes.

Is there a way, like with an attribute for example, to force the
compiler not to inline certain functions?

Also, clearly reflection has to work in release builds. So it's
possible in a release build to know the name of a method in a class.
It's also possible to get the stack frame, but without the name
property filled in. Is there a way to correlate the two? Some kind of
handle that is set in the release build stack frame that can be
compared to something in reflection so I can get the name, like a
bridge between the stack frame and reflection?

One of the requirements of what I'm doing is that they want tracing and
performance monitoring to be as unobtrusive as possible while still
working with release builds in production.

tia, brian
 
M

Mattias Sjögren

I'm trying to write a function that can reliably know the name of the
calling method.

The most reliable way is still to pass it in as a parameter.

Is there a way, like with an attribute for example, to force the
compiler not to inline certain functions?

Yes:

using System.Runtime.CompilerServices;
....
[MethodImpl(MethodImplOptions.NoInlining)]

Is there a way to correlate the two?

I doubt it. If there was, it would probably already have been taken
care of.



Mattias
 
B

brianlanning

I doubt it. If there was, it would probably already have been taken

Before, I was relying on the information in msdn. So I assumed that it
was correct when it said that GetMethod().Name wouldn't work in release
builds.

So I tried this:

private void Form1_Load(object sender, System.EventArgs e)
{
one();
}

private void one()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
MessageBox.Show(sf.GetMethod().Name);
}

.... to verify that it was in fact not working. As it turns out, it
works fine, even as a release build. This seems to contradict the msdn
and some other newsgroup posts I've seen.

I discovered that st.ToString() returns the entire stack frame, method
names and all, even in release mode. I was going to parse this string
when I discovered that Name works anyway. What's going on here? Was
this broken in the 1.0 frame work and now works in 1.1? Am I getting
lucky and sometimes it works sometimes not? Is there a compiler switch
somewhere that removes even more information? Whats going on?

brian
 

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

Top