I am trying to use expressions w/ lambda delegates to obtain the name of the calling method but it is not formatting it properly.
Here is what I have so far: Question is.. how do I get what I am to expect similar to foo.Method.Name for both lambda and regular methods?
So far, I have tried with and without expressions.. and get the same results.
< 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();
}
Here is a test driver I can show in a simple case of what I would expect. The 3x methods I need to support are:
Func<T, TR>()
Func<T, TR>(T foo)
Func<T, TR>(out T foo)
Example: Lambda expressions are nameless. It will show up something like < No Name>.
.Method.Name is correct, but since it is a sub-method of its Parent within the calling scope, it actually is registered on the stack, as follows:
< HandleAddedDevice >b__2d
I read here that I might need to make it an expression and then Expression.Compile() to convert it to an Action (or in my case Func)?
They said it may not be possible without a compiled expression here... Maybe this will help you to explain to me where my code is a bit off in what I am trying to do.
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();
}
OUTPUT
lambda(4) = True : <Main>b__0
del (4) = True : <Main>b__1
expr1 (4) = True : lambda_method
= lambda
= del
Press any key to continue...