Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiling a lambda expression results in delegate with Closure argument
    primarykey
    data
    text
    <p>When I use <code>Expression.Lambda( ... ).Compile()</code> in order to create a delegate from an expression tree, the result is a delegate of which the first argument is <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.closure.aspx"><code>Closure</code></a>.</p> <pre><code>public static Func&lt;T, T, T&gt; CreateTest&lt;T&gt;() { ParameterExpression a = Expression.Parameter( typeof( T ) ); ParameterExpression b = Expression.Parameter( typeof( T ) ); Expression addition = Expression.Add( a, b ); return (Func&lt;T, T, T&gt;)Expression.Lambda( addition, a, b ).Compile(); } ... // 'addition' equals // Int32 lambda_method( // System.Runtime.CompilerServices.Closure, // Int32, // Int32 ) Func&lt;int, int, int&gt; addition = DelegateHelper.CreateTest&lt;int&gt;(); int result = addition( 5, 5 ); </code></pre> <p>I can easily call the delegate through ordinary code without passing a <code>Closure</code> object, but where does this <code>Closure</code> come from?</p> <p>How can I call this delegate dynamically?</p> <pre><code>// The following does not work. // Exception: MethodInfo must be a runtime MethodInfo object. MethodInfo additionMethod = addition.Method; int result = (int)additionMethod.Invoke( null, new object[] { 5, 5 } ); </code></pre> <p>Using expression trees it looks like I have to pass the <code>Closure</code> object.</p> <pre><code>PropertyInfo methodProperty = typeof( Delegate ).GetProperty( "Method", typeof( MethodInfo ) ); MemberExpression getDelegateMethod = Expression.Property( Expression.Constant( addition ), methodProperty ); Func&lt;MethodInfo&gt; getMethodInfo = (Func&lt;MethodInfo&gt;)Expression.Lambda( getDelegateMethod ).Compile(); // Incorrect number of arguments supplied for call to method // 'Int32 lambda_method(System.Runtime.CompilerServices.Closure, Int32, Int32)' Expression call = Expression.Call( getMethodInfo(), Expression.Constant( 5 ), Expression.Constant( 5 ) ); </code></pre> <p>This is a simplified example which doesn't make sense in its own right. What I am actually trying to achieve is to be able to wrap e.g. <code>Func&lt;Action&lt;SomeObject&gt;&gt;</code> with <code>Func&lt;Action&lt;object&gt;&gt;</code>. I can already do this for non nested delegates. This is useful during reflection, <a href="http://whathecode.wordpress.com/2011/07/02/casting-to-less-generic-types/">as discussed here</a>.</p> <p>How should I correctly initialize this <code>Closure</code> object, or how do I prevent it from being there?</p>
    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.
    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