开发者

function to get the name of any method?

I would like to have a function called GetMethodName such that the following code would print "myMethod":

int myMeth开发者_如何转开发od(string foo, double bar)
{
    // ...
}

Console.Out.WriteLine(GetMethodName(myMethod));

This should work no matter what the method signature myMethod has. Is this possible?


No, it's not possible like that. It would be possible with the mythical infoof operator which the C# team would like to include, but haven't got round to yet - but without that, you'd have to use a method group conversion, which will only work if you know the specific type of delegate to use.

The closest you can probably come is to use an expression tree:

public static string GetMethodName(Expression expression)
{
    // Code to take apart the expression tree and find the method invocation
}

GetMethodName(() => myMethod(0, 0));

That wouldn't actually need to call myMethod, but you would need to provide dummy arguments - which could be irritating if there are any out/ref parameters.


As pointed out on Eric Lippert's blog you could fake it with the Action and Func delegates

public static MethodInfo GetInfo<T>(Action<T> action)
{
    return action.Method;
}
public static MethodInfo GetInfo<T, TResult>(Func<T, TResult> func)
{
    return func.Method;
}
public static MethodInfo GetInfo<T, U, TResult>(Func<T, U, TResult> func)
{
    return func.Method;
}   
public static int Target(int v1, int v2)
{
    return v1 ^ v2;
} 
static int Main(string[] args)
{
    var mi = GetInfo<string[], int>(Main);
    Console.WriteLine(mi.Name);

    var mi2 = GetInfo<int, int, int>(Target);
    Console.WriteLine(mi2.Name);
    return 0;
}


What about this?

public static string GetMethodName(Expression<Action> exp)
{
    var b = (MethodCallExpression)exp.Body;
    return b.Method.Name;
}

// ...


var name = GetMethodName(() => myMethod(string.Empty, 0.0));
System.Out.WriteLine(name);


Something like the below?

new StackFrame(1, false).GetMethod().Name

:Edit to add code sample...

    public string PrintMethodName()
    {
        return new StackFrame(1, false).GetMethod().Name;
    }

    private void Hi()
    {
        Console.WriteLine(PrintMethodName());
    }


EDIT: Misunderstood the question. It actually helps reading the question.

You can use Reflection:

MethodBase.GetCurrentMethod returns the current running method.


Yes it is doable. Look at class StackFrame and its GetMethod.

EDIT: looks like I misunderstood the original question. Never mind my original answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜