user2088807
user2088807

Reputation: 1408

Get Current Method Name

I want to have the name of the current Handler being called.

MethodInfo.GetCurrentMethod().Name or MethodBase.GetCurrentMethod().Name work fine in debug mode.

But once I obfuscate (using confuserEx) my project the 2 functions return "System.Reflection.MethodBase ()".

I've noticed I could get the name of my function using the following line : ((RoutedEventHandler)this.MyMethodName).GetMethodInfo().Name

It returns "MyMethodName" which is the expected result.

But it's not generic at all. I'd like a piece of code working when I don't know the current method's name.

Upvotes: 12

Views: 14956

Answers (1)

L-Four
L-Four

Reputation: 13531

As stated here:

Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time. Unlike the results of the StackTrace property for exceptions, the results aren't affected by obfuscation.

So from your method you could try to call the following method like:

public string GetCaller([System.Runtime.CompilerServices.CallerMemberName] string memberName = "")
{
     return memberName;
}

Upvotes: 20

Related Questions