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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:35:06+00:00 2026-06-02T04:35:06+00:00

I have a situation where I have an object of type dynamic that is

  • 0

I have a situation where I have an object of type “dynamic” that is invokable. I’m trying to implement a function that will invoke the object dynamically at runtime with a set of arguments. It is easy enough to invoke it if I know the number of arguments at compile time:

Object StaticInvoke(dynamic func, Object[] args) {
    // arguments are explicitly unwrapped and bound to the invokation
    // requiring that we know the number at compile time
    return func(args[0], args[1], ...); 
}

But, this doesn’t accomplish what I want because the args array could be of variable length. I can’t figure out a way to support an arbitrary number of arguments. The closest I’ve come up with is something like:

static Object Invoke(dynamic func, Object[] args)
{

    // not sure what to put here
    switch(args.Length)
    {
        case 0:
            return func();
        case 1:
            return func(args[0]);
        case 2:
            return func(args[0], args[1]);
        ...
        default:
            throw new NotSupportedException();
    }
}

Which would limit the number of arguments I could possibly pass. I guess I could live with this limitation, but it feels like what I’m trying to do should be possible.

I’m basically trying to implement the javascript “apply” function in C#. Is there an easier way to accomplish what I’m trying to do? I feel like I’m missing something obvious.

  • 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-06-02T04:35:09+00:00Added an answer on June 2, 2026 at 4:35 am

    The opensource apache licensed ImpromptuInterface (on nuget) has a static method Impromptu.Invoke that does this. You can look at the implementation, while it handles an arbitrary number of arguments, it also does work that you probably don’t need such as providing a mechanism to dynamically described named arguments and invoke them.

    Your switch case idea might actually be the way to go depending on your usage as it should be both simple and fast.

    With your switch case, with N being the largest number of arguments you support, the C# compiler will create N+1 callsites that it just statically knows where they are. Impromptu will being calling the dlr apis, just like the compiler, but it will be storing the callsite in a hashtable which will add look up time (not bad though).

    The dlr api’s also require a delegate the has a signature that matches the number or arguments your are calling, again the compiler will make sure you statically have N+1 delegates, while Impromptu will use the 17 built-in Func<> in a switch case and then start emiting new delegates as needed, it’s a one time heavy cost to generate and then adds more hashtable look ups for subsequent calls.

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

Sidebar

Related Questions

I have a situation where I have an object that is loaded back from
I'm thinking about a situation where I have an object Transaction, that has quite
I have the following situation: I have a object of type MyClass , which
I have a situation where I get a reference to an object that I
I have a situation where i want to pass a object Type as a
I have this situation: object(stdClass)#203 (1) { [1]=> object(stdClass)#212 (7) { [user_id]=> int(1) [type]=>
I have a member function with an object type as the return value type:
I have a situation where two objects of the same type have parents of
I have a situation whereby a thread can retrieve an object from the db,
Situation:- In my code I have to use the LWUIT Component object for the

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.