Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Method Name

Tags:

methods

c#

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.

like image 835
user2088807 Avatar asked Dec 08 '15 13:12

user2088807


People also ask

How can I find the method that called the current method Java?

getMethodName() method returns the name of the method containing the execution point represented by this stack trace element. Thread. currentThread().

What is MethodInfo C#?

The MethodInfo class represents a method of a type. You can use a MethodInfo object to obtain information about the method that the object represents and to invoke the method.


1 Answers

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;
}
like image 128
L-Four Avatar answered Sep 28 '22 03:09

L-Four