C# 方法名ToString()

C# 方法名ToString(),c#,.net-4.0,C#,.net 4.0,其中“GetAll”是会话密钥。 我怎么能做这样的事 GetCached(BLCustomer.GetAll, "GetAll"); 更新: 其他世界我想从方法名BLCustomer.GetAll()中获取字符串“GetAll”(不是客户名称,而是方法名称) 我想用这样的东西 GetCached(BLCustomer.GetAll, BLCustomer.GetAll.ToString()); 而不是 GetCached(BLCustomer.GetSingle, BLCustomer.Ge

其中
“GetAll”
是会话密钥。 我怎么能做这样的事

GetCached(BLCustomer.GetAll, "GetAll");
更新:

其他世界我想从方法名
BLCustomer.GetAll()
中获取字符串
“GetAll”
(不是客户名称,而是方法名称)

我想用这样的东西

GetCached(BLCustomer.GetAll, BLCustomer.GetAll.ToString());
而不是

GetCached(BLCustomer.GetSingle, BLCustomer.GetSingle.ToString());

避免硬编码方法名称。

简单解决方案:(从下面的leppie偷来)

只需从
GetCached
方法中删除第二个参数:

GetCached(BLCustomer.GetSingle, "GetSingle");
不是这样的:

ReturnType GetCached(Func<T> func)
{
    var name = func.Method.Name;

    // execute func here
}
GetCached(BLCustomer.GetAll);
GetCached(() => BLCustomer.GetAll());
string GetMethodName(Expression<Func<Func<dynamic>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}
string GetMethodName<T>(Expression<Func<Func<T>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}
GetCached<IEnumerable<Customer>>(() => BLCustomer.GetAll)
GetCached<Customer>(() => BLCustomer.GetSingle)

复杂解决方案:

您可以这样做:

ReturnType GetCached(Func<T> func)
{
    var name = func.Method.Name;

    // execute func here
}
GetCached(BLCustomer.GetAll);
GetCached(() => BLCustomer.GetAll());
string GetMethodName(Expression<Func<Func<dynamic>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}
string GetMethodName<T>(Expression<Func<Func<T>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}
GetCached<IEnumerable<Customer>>(() => BLCustomer.GetAll)
GetCached<Customer>(() => BLCustomer.GetSingle)
这种方法有两个假设:

  • 调用始终需要与示例中的类似,即它不能有参数,并且委托体必须只包含您希望从中获取名称的方法,而不包含其他内容
  • 要从中获取名称的方法不能是类型
    void
    ,也不能有任何参数
  • 您也可以将其用于非静态方法:

    GetCached(BLCustomer.GetSingle, GetMethodName(() => BLCustomer.GetSingle));
    
    您甚至可以将
    GetCached
    更改为以下内容以清理其API:

    BLCustomer customer = new BLCustomer();
    GetCached(customer.GetSingle, GetMethodName(() => customer.GetSingle));
    
    然后你可以这样称呼它:

    ReturnType GetCached(Func<T> func)
    {
        var name = func.Method.Name;
    
        // execute func here
    }
    
    GetCached(BLCustomer.GetAll);
    
    GetCached(() => BLCustomer.GetAll());
    
    string GetMethodName(Expression<Func<Func<dynamic>>> methodExpression)
    {
        dynamic memberExpression = methodExpression.Body;
        MethodInfo result = memberExpression.Operand.Arguments[2].Value;
        return result.Name;
    }
    
    string GetMethodName<T>(Expression<Func<Func<T>>> methodExpression)
    {
        dynamic memberExpression = methodExpression.Body;
        MethodInfo result = memberExpression.Operand.Arguments[2].Value;
        return result.Name;
    }
    
    GetCached<IEnumerable<Customer>>(() => BLCustomer.GetAll)
    GetCached<Customer>(() => BLCustomer.GetSingle)
    
    GetCached(()=>BLCustomer.GetAll)
    GetCached(()=>BLCustomer.GetSingle)
    
    如下更改GetCached:

    ReturnType GetCached(Func<T> func)
    {
        var name = func.Method.Name;
    
        // execute func here
    }
    
    GetCached(BLCustomer.GetAll);
    
    GetCached(() => BLCustomer.GetAll());
    
    string GetMethodName(Expression<Func<Func<dynamic>>> methodExpression)
    {
        dynamic memberExpression = methodExpression.Body;
        MethodInfo result = memberExpression.Operand.Arguments[2].Value;
        return result.Name;
    }
    
    string GetMethodName<T>(Expression<Func<Func<T>>> methodExpression)
    {
        dynamic memberExpression = methodExpression.Body;
        MethodInfo result = memberExpression.Operand.Arguments[2].Value;
        return result.Name;
    }
    
    GetCached<IEnumerable<Customer>>(() => BLCustomer.GetAll)
    GetCached<Customer>(() => BLCustomer.GetSingle)
    
    假设:

    我想,
    GetCached
    当前实际上是这样的:

    ReturnType GetCached(SomeFunc f)
    {
      var methodname = f.Method.Name;
      // add rest of code
    }
    

    GetAll
    返回什么类型?BLCustomer中的GetAll是什么?我有一个问题。是
    BLCustomer.GetSingle
    BLCustomer.GetAll
    静态属性吗?在
    UPDATE:
    顶部的
    是什么意思?@johnycagewins,它表示问题的标志。你能详细说明一下吗?我不明白。什么是
    SomeFunc
    ?@Daniel Hilgarth:我假设的某个委托没有
    GetCached
    方法的签名。@Daniel Hilgarth:更新了我的答案,现在应该更清楚了:)你的方法越来越接近我的建议。我不认为您真的需要使用
    表达式
    ,除非您涉及闭包(根据您的示例)。再说一次,我可能做了太多的假设:)@leppie:我不知道这是可能的。。。你的答案应该是被接受的!正如我目前的回答一样,我将更新我的答案,以展示如何用简单的方式来做,因为我认为你的答案有点短:-)谢谢,但我仍然觉得这里有太多的假设要给出准确的答案:)@leppie:你是对的。但我认为我们现在向他展示了所有的可能性;-)你能指出
    GetCached(BLCustomer.GetAll)之间的区别吗
    GetCached(()=>BLCustomer.GetAll())我一直认为这是完全一样的,但似乎不是。