我正在尝试使用带有 lambda 委托的表达式来获取调用方法的名称,但它的格式不正确。
到目前为止,这是我所拥有的:问题是..对于 lambda 和常规方法,我如何获得类似于 foo.Method.Name 的期望?
到目前为止,我已经尝试过使用和不使用表达式......并得到相同的结果。
< HandleAddedDevice >b__2d
// **************************************************************************
public delegate TResult TimerDelegateOut <T, out TResult>(out T foo);
// **************************************************************************
public static string GetName<T>(this Expression<T> expression) {
var callExpression = expression.Body as MethodCallExpression;
return callExpression != null ? callExpression.Method.Name : string.Empty;
}
// **************************************************************************
public static Expression<TimerDelegateOut<T, TResult>> ToExpression<T, TResult>(this TimerDelegateOut<T, TResult> call) {
var p1 = Expression.Parameter(typeof(T).MakeByRefType(), "value");
MethodCallExpression methodCall = call.Target == null
? Expression.Call(call.Method, p1)
: Expression.Call(Expression.Constant(call.Target), call.Method, p1);
return Expression.Lambda<TimerDelegateOut<T, TResult>>(methodCall, p1);
}
// **************************************************************************
public static Expression<Func<TResult>> ToExpression<TResult>(this Func<TResult> call) {
MethodCallExpression methodCall = call.Target == null
? Expression.Call(call.Method)
: Expression.Call(Expression.Constant(call.Target), call.Method);
return Expression.Lambda<Func<TResult>>(methodCall);
}
// **************************************************************************
public static TResult TimeFunction<T, TResult>(TimerDelegateOut<T, TResult> foo, out T bar) {
try {
var result = foo.ToExpression().Compile().Invoke(out bar);
Console.WriteLine(foo.GetName()); // is OKAY
return result;
} catch (Exception) {
bar = default(T);
return default(TResult);
}
}
// **************************************************************************
public static TResult TimeFunction<TResult>(Func<TResult> foo) {
try {
var result = foo.ToExpression().Compile().Invoke();
Console.WriteLine(foo.GetName()); // <-- prints "foo" ??? Not correct.
return result;
} catch (Exception) {
bar = default(T);
return default(TResult);
}
}
-------------
Result GetCamera_HWInfo(out Cam_HWInfo obj)
{
obj = new Cam_HWInfo() { < fill container here > };
return Result.cmrOk;
}
//------------
private void HandleAddedDevice() {
...
Cam_HWInfo camHWInfo;
Result result = Watchdog.TimeFunction(GetCamera_HWInfo, out camHWInfo);
...
// Try this one.. I am also using.
var connect = new Func<bool>(delegate {
try {
// ...
} catch (Exception ex) {
return false;
}
return true;
});
result = Watchdog.TimeFunction(connect);
}
//------------
// Assume OEP
static void Main(string[] args)
{
HandleAddedDevice();
}
这是一个测试驱动程序,我可以在一个简单的情况下展示我所期望的。我需要支持的 3x 方法是:
Func<T, TR>()
Func<T, TR>(T foo)
Func<T, TR>(out T foo)
示例: Lambda 表达式是无名的。它将显示类似 <No Name> 的内容。
.Method.Name是正确的,但是由于是其Parent在调用范围内的子方法,所以实际上是在栈上注册的,如下:
< HandleAddedDevice >b__2d
我在这里读到,我可能需要将其设为表达式,然后将 Expression.Compile() 转换为 Action(或者在我的情况下为 Func)?
他们说这里没有编译表达式可能是不可能的......也许这会帮助你向我解释我的代码在我想做的事情中有点偏离。
class Program {
public static class ReflectionUtility {
public static string GetPropertyName<T>(Expression<Func<T>> expression) {
MemberExpression body = (MemberExpression) expression.Body;
return body.Member.Name;
}
}
static void Main(string[] args) {
Func<int, bool> lambda = i => i < 5;
Func<int, bool> del = delegate(int i) { return i < 5; };
// Create similar expression #1.
Expression<Func<int, bool>> expr1 = i => i < 5;
// Compile the expression tree into executable code.
Func<int, bool> exprC1 = expr1.Compile();
// Invoke the method and print the output.
Console.WriteLine("lambda(4) = {0} : {1} ", lambda(4), lambda.Method.Name);
Console.WriteLine("del (4) = {0} : {1} ", del(4), del.Method.Name);
Console.WriteLine("expr1 (4) = {0} : {1} ", exprC1(4), exprC1.Method.Name);
Console.WriteLine(" = {0}", ReflectionUtility.GetPropertyName(() => lambda));
Console.WriteLine(" = {0}", ReflectionUtility.GetPropertyName(() => del));
Console.Write("Press any key to continue...");
Console.ReadKey();
}
输出
lambda(4) = True : <Main>b__0
del (4) = True : <Main>b__1
expr1 (4) = True : lambda_method
= lambda
= del
Press any key to continue...