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

  • Home
  • SEARCH
  • 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 904675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:05:34+00:00 2026-05-15T16:05:34+00:00

Okay first to explain the rules: I need a function that constructs a delegate

  • 0

Okay first to explain the rules:

I need a function that constructs a delegate matching any delegate type that encapsulates a body of which invokes a delegate of type (Object) (Object[] args) with ‘args’ containing all of the arguments passed to the original delegate during invocation.

My work so far:

    delegate void TestDelegate(int x, int y);
    delegate object TestDelegate2(object[] args);

    static void Main(string[] sargs)
    {
        TestDelegate d = (TestDelegate)CreateAnonymousFromType(typeof(TestDelegate));
        object ret = d.DynamicInvoke(2, 6);

        if (ret != null) { Console.WriteLine(ret); }

        Console.ReadKey();
    }

    static void SpecialInvoke(int x, int y) 
    {
        Console.WriteLine("x: {0}\r\ny: {1}", x, y);
    }

    static Delegate CreateAnonymousFromType(Type type)
    {
        MethodInfo method = type.GetMethod("Invoke");

        TestDelegate2 _delegate = new TestDelegate2(
            delegate(object[] args) 
            {
                Console.WriteLine("x: {0}\r\ny: {1}", args[0], args[1]);
                return "This is the return";
            });


        var typeargs = CreateArgs(method.GetParameters());
        var argindex = -1;

        var tmp = Expression.Parameter(typeof(Object), "tmp");
        var index = Expression.Parameter(typeof(int), "index");

        var length = Expression.Constant(typeargs.Length);

        var _break = Expression.Label("breakto");

        var delegateargs = Expression.Parameter(typeof(object[]), "args");

        return Expression.Lambda(
            type,
            Expression.Block(
                new[] { tmp, index, delegateargs },
                Expression.Assign(index, Expression.Constant(0)),
                Expression.Assign(delegateargs, Expression.NewArrayBounds(typeof(Object), length)),
                Expression.Loop(
                    Expression.IfThenElse(Expression.LessThan(index, length),
                        Expression.Block(
                            Expression.Assign(tmp, Expression.Convert(typeargs[++argindex], typeof(Object))),
                            Expression.Assign(Expression.ArrayAccess(delegateargs, index), tmp),
                            Expression.PostIncrementAssign(index)
                        ),
                        Expression.Break(_break)
                    ),
                    _break
                ),
                Expression.Call(_delegate.Method, delegateargs)
            ),
            typeargs
        ).Compile();
    }

    static ParameterExpression[] CreateArgs(ParameterInfo[] _params)
    {
        ParameterExpression[] ret = new ParameterExpression[_params.Length];

        for (int i = 0; i < ret.Length; i++)
            ret[i] = Expression.Parameter(_params[i].ParameterType, _params[i].Name);

        return ret;
    }

Now this SORTA works… I only get the value of typeargs[0] passed to the delegate “TestDelegate2” for both parameters x and y, the ‘args’ parameter at runtime is object[] { 2, 2 } and I can’t for the life of me find a way to increment “argindex” inside the scope of the argument iteration… parameter count at compile time is indefinate.
Anybody know how I can solve this?

I’ve tried just copying the argument array using Expression.NewArrayInit(typeof(Object), typeargs) but that fails saying it can’t use Int32 to initialize an array of Object

I’ve also tried this:
var arguments = Expression.Constant(typeargs);

And accessing the value of “arguments” at “index”, however this produces the strings “x” and “y” .. apparently the names of the arguments and not their values.

This is honestly my first major attempt at using expression trees so any help.. no matter how little. Would be appreciated.

Thank you.

  • 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-15T16:05:35+00:00Added an answer on May 15, 2026 at 4:05 pm

    I think you were on the right track with Expression.NewArrayInit. You can fix the “An expression of type ‘System.Int32’ cannot be used to initialize an array of type ‘System.Object'” error by using Expression.Convert to insert a conversion for each parameter:

    var typeargs = CreateArgs(method.GetParameters());
    return Expression.Lambda(
        type,
        Expression.Call(_delegate.Method, Expression.NewArrayInit(typeof(object),
            typeargs.Select(arg => Expression.Convert(arg, typeof(object)))
            )),
        typeargs
    ).Compile();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would suggest to use Spring Framework. Using it you… May 16, 2026 at 11:23 am
  • Editorial Team
    Editorial Team added an answer Yes, you need just 3 days for sha1(salt | password).… May 16, 2026 at 11:23 am
  • Editorial Team
    Editorial Team added an answer Using Html.Serialize helper method to persist view models between pages… May 16, 2026 at 11:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Okay, here's what I'm trying to do. First I'll explain the end result I'm
First of all, I am using PhpMyAdmin , is this okay or not? Because
Can you please explain me the difference between mechanism of the following: int function();
I have another programmer who I'm trying to explain why it is that a
I stumbled upon an interesting error that I've never seen before, and can't explain
Here is a flow that I can not figure out how to work. (
Okay, this is quite an interesting challenge I have got myself into. My RegEx
Okay so the array is $nv = array(); $nv[$kk] = $value; And index key
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;

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.