Get Name of Action/Func Delegate

前端 未结 2 1635
说谎
说谎 2020-12-24 01:20

I have a weird situation where I need to get the Name of the delegate as a string. I have a generic method that looks like this.

private T Get(T tas         


        
2条回答
  •  粉色の甜心
    2020-12-24 01:24

    You can get the name of the method call by making the parameter an expression instead of a delegate, just like Jon mentioned

    private T Get(T task, Expression> method) where T : class
    {
        if (method.Body.NodeType == ExpressionType.Call)
        {
            var info = (MethodCallExpression)method.Body;
            var name = info.Method.Name; // Will return "Bark"
        }
    
        //.....
    }
    

提交回复
热议问题