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 5989475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:06:23+00:00 2026-05-22T23:06:23+00:00

I’m working on a small programming language for the Microsoft DLR, and having a

  • 0

I’m working on a small programming language for the Microsoft DLR, and having a bit of a problem invoking my anonymous methods. Specifically, the code:

Delegate CompiledBody = Expression.Lambda(rt.Parser.ParseSingle(Body), parms).Compile();

So, parms is an array containing a single ParameterExpression, and the first argument contains the appropriate Expressions to define the anonymous function. When I try to invoke my Delegate using an Expression.Call on CompiledBody.Method (a MethodInfo), I receive the error:

Unhandled Exception: System.ArgumentException: Expression of type 'System.Object' 
cannot be used for parameter of type 'System.Runtime.CompilerServices.Closure' 
of method 'Shiro.Runtime.ShiroAtom lambda_method(System.Runtime.CompilerServices
.Closure, Shiro.Runtime.ShiroAtom)'

Now, somewhere along the way my one-argument method gained a second argument, of type System.Runtime.CompilerServices.Closure (the second one, of type ShiroAtom, is my parameter). This makes sense, except that (a) I don’t really care if the method in this context is in a Closure scope and (b) I can’t seem to create even an empty Closure scope to pass in this parameter.

I’d appreciate any help! Thanks in advance.

EDIT: Some additional information based on the awesome reply below:

Where this code occurs is deep in the bowels of my Parser. I have a stream of tokens (actually, Atoms) which get translated into an AST. This particular bit is the function call parse routine. It created a CompiledBody, then tries to invoke it using something like:

return Expression.Call(CompiledBody.Method, Expression.Constant("argument"));

The resulting Lambda represents a function. Based on my architecture there are only a few places I can call DynamicInvoke or just calling the Compiled Delegate, and this isn’t one of them. I wish I could provide a more substantial example but this situation occurs in the midst of a hand-coded parser, and it would take way too much code to really communicate why the situation is this way, but I really need a way to call the compiled Lambda via Expression.Call, as shown above.

The crux of the problem is that my Compiled Lambda requires 1 additional parameter to the ones I specify, a CompilerServices.Closure, and I don’t know how to make one.

  • 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-22T23:06:24+00:00Added an answer on May 22, 2026 at 11:06 pm

    It would be helpful if you could share the body that you’re compiling as that would contain the actual closure and how you’re invoking it. My guess is that you are attempting to invoke the resulting delegate “by hand” somehow instead of holding onto something of the delegate object and simply generating an Invoke expression. If you want to use the DLR closures this is how it should look:

    using System;
    using System.Linq.Expressions;
    
    class Program {
        static void Main(string[] args) {
            var outerParam = Expression.Parameter(typeof(int), "outerParam");
    
            var lambda =
                Expression.Lambda<Func<int, Action>>(
                    Expression.Lambda<Action>(
                        Expression.Call(
                            typeof(Console).GetMethod("WriteLine", new Type[] { typeof(object) }),
                            Expression.Convert(outerParam, typeof(object))
                        )
                    ),
                    outerParam
                ).Compile();
    
            var actionParam = Expression.Parameter(typeof(Action), "action");
            var lambdaInvoker =
                Expression.Lambda<Action<Action>>(
                    Expression.Invoke(actionParam),
                    actionParam
                ).Compile();
    
            lambdaInvoker(lambda(100));
            lambdaInvoker(lambda(200));
            Console.ReadLine();
        }
    }
    

    That creates 3 lambdas: The 1st one contains a 2nd inner lambda which closes over a parameter. The type of the resulting closure delegate is the type specified when creating the lambda expression even though there’s an extra hidden parameter there. The 3rd lambda shows how you can invoke this from another lambda – that is via a delegate invocation. Finally we chain the delegates together to show how it works.

    Also one thing to be aware of us that DLR closures don’t actually perform that great right now due to a limitation in the CLR. Creating the closure is actually a pretty slow process because it needs to go through reflection instead being able to create the delegate directly. If you’re concerned about performance of delegate you’ll want to track the variables and flow closed over values in via your own data structure (this is what both IronRuby and IronPython do).

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

Sidebar

Related Questions

No related questions found

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.