Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6750641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:49:53+00:00 2026-05-26T12:49:53+00:00

When I use Expression.Lambda( … ).Compile() in order to create a delegate from an

  • 0

When I use Expression.Lambda( ... ).Compile() in order to create a delegate from an expression tree, the result is a delegate of which the first argument is Closure.

public static Func<T, T, T> CreateTest<T>()
{
    ParameterExpression a = Expression.Parameter( typeof( T ) );
    ParameterExpression b = Expression.Parameter( typeof( T ) );
    Expression addition = Expression.Add( a, b );

    return (Func<T, T, T>)Expression.Lambda( addition, a, b ).Compile();
}

...

// 'addition' equals
// Int32 lambda_method(
//     System.Runtime.CompilerServices.Closure,
//     Int32,
//     Int32 )
Func<int, int, int> addition = DelegateHelper.CreateTest<int>();
int result = addition( 5, 5 );

I can easily call the delegate through ordinary code without passing a Closure object, but where does this Closure come from?

How can I call this delegate dynamically?

// 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 } );

Using expression trees it looks like I have to pass the Closure object.

PropertyInfo methodProperty
    = typeof( Delegate ).GetProperty( "Method", typeof( MethodInfo ) );
MemberExpression getDelegateMethod
    = Expression.Property( Expression.Constant( addition ), methodProperty );
Func<MethodInfo> getMethodInfo
    = (Func<MethodInfo>)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 ) );

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. Func<Action<SomeObject>> with Func<Action<object>>. I can already do this for non nested delegates. This is useful during reflection, as discussed here.

How should I correctly initialize this Closure object, or how do I prevent it from being there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:49:54+00:00Added an answer on May 26, 2026 at 12:49 pm

    The Closure type you see is an implementation detail. The MSDN is pretty explicit about it:

    This API supports the .NET Framework infrastructure and is not
    intended to be used directly from your code. Represents the runtime
    state of a dynamically generated method.

    An expression tree can have a state.

    The Closure instance will contain all the non literal constants that the lambda expression, well, closes over. It can also contain a chain of delegates for nested lambdas in expression trees.

    To achieve this, the expression tree compiler uses a cute little trick. It generates in memory code using a DynamicMethod, that is by definition static. Yet, they’re creating a delegate that is “closed over its first argument”. Meaning that the CLR will pass the target field of the delegate as a first argument of the static method, so you don’t have to. Effectively hiding the Closure argument from you.

    The solution to your problem is simple, don’t try to call the method, invoke the delegate, either by using Delegate.DynamicInvoke when you’re using reflection, or Expression.Invoke in the context of an expression tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can I create a dynamic lambda expression to pass to use in my
I'm trying to use a lambda expression to remove a certain object from a
What are the different scenario in which we can use Lambda Expression. One of
Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a
If I use a lambda expression like the following // assume sch_id is a
I'm trying to use the Expression tree and Lamdba Expression objects in .Net 3.5
Can you use a lambda expressions as an argument to an attribute? The motivation
You can use Lambda Expression Objects to represent a lambda as an expression. How
How to use lambda expression as a template parameter? E.g. as a comparison class
I have public delegate void DocumentCompleteEventHandler(object pDisp, ref object URL) Can i use lambda

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.