我正在尝试使用.Net闭包将函数的方法名称传递给对象,如下所示:
方法签名
public IEnumerable<T> GetData<T>(Func<IEnumerable<T>> WebServiceCallback)
where T : class
{
// either gets me '<LoadData>b__3'
var a = nrdsWebServiceCallback.Method.Name;
var b = nrdsWebServiceCallback.GetInvocationList();
return WebServiceCallback();
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它:
SessionStateService.Labs = CacheManager.GetData(() =>
WCFService.GetLabs(SessionStateService.var1, SessionStateService.var2));
Run Code Online (Sandbox Code Playgroud)
看到'b__3'而不是WCFServce.GetLabs(..)等
dca*_*tro 15
您现在看到的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()();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5413 次 |
| 最近记录: |