我正在尝试使用.Net闭包将函数的方法名称传递给对象,如下所示:
方法签名
public IEnumerableGetData (Func > WebServiceCallback) where T : class { // either gets me ' b__3' var a = nrdsWebServiceCallback.Method.Name; var b = nrdsWebServiceCallback.GetInvocationList(); return WebServiceCallback(); }
我这样称呼它:
SessionStateService.Labs = CacheManager.GetData(() => WCFService.GetLabs(SessionStateService.var1, SessionStateService.var2));
看到'b__3'而不是WCFServce.GetLabs(..)等
您现在看到的lambda表达式(编译器生成)的名称,而不是所谓的拉姆达内部的方法的名称.
你必须使用<Expression<Func<T>>
而不是Func<T>
.表达式可以被解析和分析.
尝试
public IEnumerable<T> GetData<T>(Expression<Func<IEnumerable<T>>> callbackExpression) where T : class { var methodCall = callbackExpression.Body as MethodCallExpression; if(methodCall != null) { string methodName = methodCall.Method.Name; } return callbackExpression.Compile()(); }