Paul_87
Paul_87

Reputation: 117

A shorter way to write System.Reflection.MethodBase.GetCurrentMethod.Name?

I've been focusing on logging more lately. I really do appreciate getting the method name in the logger. My only issue is that the reflective method is so long to write.

I tested with static classes and a method for it. But to my not so big surprise, it gave me the name of the static class method.

So far the only way I found to shorten it a bit is

using Info = System.Reflection.MethodBase;
string name = Info.GetCurrentMethod().Name;

Is there a way to shorten this to something like Info.MethodName()?

Upvotes: 0

Views: 189

Answers (1)

Paul_87
Paul_87

Reputation: 117

After following tips from Leisen Chang tips about using CallerMemberName solved it with a static class.

public static class Info
{
    public static string MethodName([System.Runtime.CompilerServices.CallerMemberName] string name = "")
    {
        return name;
    }
}

Upvotes: 1

Related Questions