Find the name of the current method using CallerMemberName attribute
The following method can be used to return the name of the current method.
using System.Runtime.CompilerServices;
public static string GetCallerName([CallerMemberName] string? caller = null)
{
return caller ?? string.Empty;
}
Usage:
// create a sample function
public static void FooFunction()
{
string temp = GetCallerName(); // Do not specify anything for the parameter. Let it default.
// at this point, the string variable 'temp' will have the value "FooFunction".
}
Related
Last modified by Mohit @ 4/16/2025 5:17:31 AM