Note that there are some explanatory texts on larger screens.

plurals
  1. POLambda Generic Expression w/ Out Parameter
    primarykey
    data
    text
    <p>I am trying to use expressions w/ lambda delegates to obtain the name of the calling method but it is not formatting it properly. </p> <p>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?</p> <p>So far, I have tried with and without expressions.. and get the same results.<br></p> <blockquote> <p>&lt; HandleAddedDevice >b__2d</p> </blockquote> <pre><code> // ************************************************************************** public delegate TResult TimerDelegateOut &lt;T, out TResult&gt;(out T foo); // ************************************************************************** public static string GetName&lt;T&gt;(this Expression&lt;T&gt; expression) { var callExpression = expression.Body as MethodCallExpression; return callExpression != null ? callExpression.Method.Name : string.Empty; } // ************************************************************************** public static Expression&lt;TimerDelegateOut&lt;T, TResult&gt;&gt; ToExpression&lt;T, TResult&gt;(this TimerDelegateOut&lt;T, TResult&gt; 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&lt;TimerDelegateOut&lt;T, TResult&gt;&gt;(methodCall, p1); } // ************************************************************************** public static Expression&lt;Func&lt;TResult&gt;&gt; ToExpression&lt;TResult&gt;(this Func&lt;TResult&gt; call) { MethodCallExpression methodCall = call.Target == null ? Expression.Call(call.Method) : Expression.Call(Expression.Constant(call.Target), call.Method); return Expression.Lambda&lt;Func&lt;TResult&gt;&gt;(methodCall); } // ************************************************************************** public static TResult TimeFunction&lt;T, TResult&gt;(TimerDelegateOut&lt;T, TResult&gt; 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&lt;TResult&gt;(Func&lt;TResult&gt; foo) { try { var result = foo.ToExpression().Compile().Invoke(); Console.WriteLine(foo.GetName()); // &lt;-- 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() { &lt; fill container here &gt; }; 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&lt;bool&gt;(delegate { try { // ... } catch (Exception ex) { return false; } return true; }); result = Watchdog.TimeFunction(connect); } //------------ // Assume OEP static void Main(string[] args) { HandleAddedDevice(); } </code></pre> <hr> <p>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:</p> <ol> <li><code>Func&lt;T, TR&gt;()</code></li> <li><code>Func&lt;T, TR&gt;(T foo)</code></li> <li><code>Func&lt;T, TR&gt;(out T foo)</code></li> </ol> <p><strong>Example:</strong> Lambda expressions are nameless. It will show up something like &lt; No Name>. </p> <p>.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:</p> <p>&lt; HandleAddedDevice >b__2d</p> <p>I read <a href="https://stackoverflow.com/questions/1495465/get-name-of-action-func-delegate">here</a> that I might need to make it an expression and then Expression.Compile() to convert it to an Action (or in my case Func)?</p> <p>They said it may not be possible without a compiled expression <a href="https://stackoverflow.com/questions/767733/converting-a-net-funct-to-a-net-expressionfunct">here</a>... Maybe this will help you to explain to me where my code is a bit off in what I am trying to do. </p> <pre><code> class Program { public static class ReflectionUtility { public static string GetPropertyName&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; expression) { MemberExpression body = (MemberExpression) expression.Body; return body.Member.Name; } } static void Main(string[] args) { Func&lt;int, bool&gt; lambda = i =&gt; i &lt; 5; Func&lt;int, bool&gt; del = delegate(int i) { return i &lt; 5; }; // Create similar expression #1. Expression&lt;Func&lt;int, bool&gt;&gt; expr1 = i =&gt; i &lt; 5; // Compile the expression tree into executable code. Func&lt;int, bool&gt; 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(() =&gt; lambda)); Console.WriteLine(" = {0}", ReflectionUtility.GetPropertyName(() =&gt; del)); Console.Write("Press any key to continue..."); Console.ReadKey(); } </code></pre> <h2> <strong>OUTPUT</strong></h2> <pre><code>lambda(4) = True : &lt;Main&gt;b__0 del (4) = True : &lt;Main&gt;b__1 expr1 (4) = True : lambda_method = lambda = del Press any key to continue... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload