Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nameof to get name of current method

Have browsed, searched and hoped but cannot find a straight answer.

Is there anyway in C# 6.0 to get the current method name using nameof withouth specifying the method name?

I am adding my test results to a dictionary like this:

Results.Add(nameof(Process_AddingTwoConsents_ThreeExpectedRowsAreWrittenToStream), result);

I would prefer if I would not have to specify the method name explicitly so I can copy+paste the line, a non-working example:

Results.Add(nameof(this.GetExecutingMethod()), result);

If possible I do not want to use Reflection.

UPDATE

This is not (as suggested) a duplicate of this question. I am asking if is explicitly possible to make use of nameof without(!) reflection to get the current method name.

like image 848
Marcus Avatar asked Jun 29 '16 11:06

Marcus


People also ask

How do you find the current method name?

Using MethodBase.GetCurrentMethod() method can be used, which returns a MethodBase object representing the currently executing method. That's all about getting the name of the current method in C#.

What does Nameof return?

C# nameof operator returns the unqualified string name of a variable, type, or member.

When was Nameof added to C#?

The nameof operator, added in C# 6.0, addresses this — it allows capturing the string names of symbols that are in the scope.


3 Answers

You can't use nameof to achieve that, but how about this workaround:

The below uses no direct reflection (just like nameof) and no explicit method name.

Results.Add(GetCaller(), result);

public static string GetCaller([CallerMemberName] string caller = null)
{
    return caller;
}

GetCaller returns the name of any method that calls it.

like image 184
Zein Makki Avatar answered Oct 19 '22 02:10

Zein Makki


Building on user3185569's great answer:

public static string GetMethodName(this object type, [CallerMemberName] string caller = null)
{
    return type.GetType().FullName + "." + caller;
}

Results in you being able to call this.GetMethodName() anywhere to return the fully qualified method name.

like image 25
Mike Cole Avatar answered Oct 19 '22 02:10

Mike Cole


Same as others, but some variation:

    /// <summary>
    /// Returns the caller method name.
    /// </summary>
    /// <param name="type"></param>
    /// <param name="caller"></param>
    /// <param name="fullName">if true returns the fully qualified name of the type, including its namespace but not its assembly.</param>
    /// <returns></returns>
    public static string GetMethodName(this object type, [CallerMemberName] string caller = null, bool fullName = false)
    {
        if (type == null) throw new ArgumentNullException(nameof(type));
        var name = fullName ? type.GetType().FullName : type.GetType().Name;
        return $"{name}.{caller}()";
    }

Allows to call it like this:

Log.Debug($"Enter {this.GetMethodName()}...");
like image 1
Chris Avatar answered Oct 19 '22 04:10

Chris