12

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.

wonea
  • 4,783
  • 17
  • 86
  • 139
user2088807
  • 1,378
  • 2
  • 25
  • 47
  • 1
    Possible duplicate of [C# how to get the name of the current method from code](http://stackoverflow.com/questions/2652460/c-sharp-how-to-get-the-name-of-the-current-method-from-code) – Thomas Ayoub Dec 08 '15 at 13:15
  • If nothing else helps, are solutions involving a preprocessor for your source code acceptable? – O. R. Mapper Dec 08 '15 at 13:15
  • 2
    @Thomas, Yahya: Not convinced this is a duplicate, as none of the linked questions specifically refers to the difficulties introduced to make this robust against code obfuscation. – O. R. Mapper Dec 08 '15 at 13:17
  • @O.R.Mapper removed it. – Yahya Dec 08 '15 at 13:19
  • If the OP is on 4.5 doesn't the `[CallerMemberName]` solves the obfuscation issue? @O.R.Mapper – rene Dec 08 '15 at 13:19
  • @rene: I don't know. The OP should try (and if it does, they can either answer their own question for future reference, or delete their question because the problem does not exist any more for themselves). Just my opinion, but in both cases, the question how to get the current method name in a way that survives obfuscation has not been asked before (at least not in the linked alleged duplicates), even though the solution happens to be the same as for existing, other questions. An alternative might be adding a comment to answers of the existing questions pointing out ... – O. R. Mapper Dec 08 '15 at 13:23
  • Thanks for the suggestion, I'm going to try and tell you. – user2088807 Dec 08 '15 at 13:24
  • ... that the respective solutions are obfuscation-safe, but I don't think that's well findable for future visitors; it might seem a bit chatty. – O. R. Mapper Dec 08 '15 at 13:24
  • Possible duplicate of [Can you use reflection to find the name of the currently executing method?](http://stackoverflow.com/questions/44153/can-you-use-reflection-to-find-the-name-of-the-currently-executing-method) – Michael Freidgeim Aug 22 '16 at 07:13

1 Answers1

20

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;
}
L-Four
  • 13,345
  • 9
  • 65
  • 109