2

I'm using Visual Studio 2010, .NET 3.5 and PostSharp 3.1.33. When I execute this code

[MyInterceptorAspect]
public void Send(string msg)
{
    Console.WriteLine(MethodBase.GetCurrentMethod().Name);
}

it prints <Send>z__OriginalMethod, but I want it to print just Send. Is it possible at all? (NB: MyInterceptorAspect extends MethodInterceptionAspect and it works flawlessly, concrete implementation is just not important for this problem)

  • is this helpful? http://stackoverflow.com/questions/2652460/c-sharp-how-to-get-the-name-of-the-current-method-from-code – Ron.B.I Mar 05 '14 at 19:00
  • @Ron.B.I Tnx, kind of. I had to use `new StackTrace().GetFrame(4).GetMethod().Name` which makes me feel a bit uncomfortable because of the magic number. Frames 1, 2 and 3 refer to some of the PostSharp's methods. – TheJavaGuy-Ivan Milosavljević Mar 05 '14 at 19:14

1 Answers1

1

Since PostSharp changes your methods after they've been compiled, one solution is to resolve it at compile time, rather than runtime.

string CurrentMethod([CallerMemberName] string caller = null)
{
    return caller;
}

Console.WriteLine(CurrentMethod());

Alternatively, you could just search the 'mangled' name using a regex, to find the original name.

string GetOriginalName(string mangled)
{
    return new Regex(@"\w+").Match(mangled).Value;
}
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148