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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:16:26+00:00 2026-05-23T10:16:26+00:00

I have MethodBases for two functions: public static int Add(params int[] parameters) { /*

  • 0

I have MethodBases for two functions:

public static int Add(params int[] parameters) { /* ... */ }
public static int Add(int a, int b) { /* ... */ }

I have a function that calls the MethodBases via a class I made:

MethodBase Method;
object Target;
public object call(params object[] input)
{
    return Method.Invoke(Target, input);
}

Now if I AddTwoMethod.call(5, 4); it works fine.

If I however use AddMethod.call(5, 4); it returns:

Unhandled Exception: System.Reflection.TargetParameterCountException: parameters do not match signature

Is there any way to make it so that both calls work fine without need for manually putting the arguments in an array for the params int[]?

  • 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-23T10:16:27+00:00Added an answer on May 23, 2026 at 10:16 am

    You could modify your call method to detect the params parameter and convert the rest of the input to a new array. That way your method could act pretty much the same as the logic C# applies to the method calling.

    Something i quicly constructed for you (be aware that i tested this method in a pretty limited way, so there might be errors still):

    public object call(params object[] input)
    {
        ParameterInfo[] parameters = Method.GetParameters();
        bool hasParams = false;
        if (parameters.Length > 0)
            hasParams = parameters[parameters.Length - 1].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0;
    
        if (hasParams)
        {
            int lastParamPosition = parameters.Length - 1;
    
            object[] realParams = new object[parameters.Length];
            for (int i = 0; i < lastParamPosition; i++)
                realParams[i] = input[i];
    
            Type paramsType = parameters[lastParamPosition].ParameterType.GetElementType();
            Array extra = Array.CreateInstance(paramsType, input.Length - lastParamPosition);
            for (int i = 0; i < extra.Length; i++)
                extra.SetValue(input[i + lastParamPosition], i);
    
            realParams[lastParamPosition] = extra;
    
            input = realParams;
        }
    
        return Method.Invoke(Target, input);
    }
    

    Be aware that i tested this method in a pretty limited way, so there might be errors still.

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

Sidebar

Related Questions

I have a class that look like the following: public class MyClass { ...
I have some code (which works fine) that looks something like this: int integer
I have a derived class with a method that overrides the base class's method,
I have an executable that depending on the command line switch supplied looks something
We have a log class which uses Relection.MethodBase to send current class info to
I have a WCF application that is using NetTcpBinding . I want to invoke
I have a interface that defines some methods with attributes. These attributes need to
I have been looking at Reflection.Emit recently. I wrote a simple program that generates
I have a method in an object that is called from a number of
Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal

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.