Search code examples
c#reflectionlate-binding

How can I pass an integer array to a method: Reflection C#


I am currently learning about Reflection in C#. I am calling 2 methods from a class using late binding. The first method (SumNumbers) works. The second method (SumArray) throws an exception saying "Parameter count mismatch". Can anyone kindly tell me how to pass an integer array to this method?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace ReflectionWithLateBinding
{
    public class Program
    {
        static void Main()
        {
            //load the current executing assembly
            Assembly executingAssembly1 = Assembly.GetExecutingAssembly();

            //load and instantiate the class dynamically at runtime - "Calculator class"
            Type calculatorType1 = executingAssembly1.GetType("ReflectionWithLateBinding.Calculator");


            //Create an instance of the type --"Calculator class"
            object calculatorInstance1 = Activator.CreateInstance(calculatorType1);

            //Get the info of the method to be executed in the class
            MethodInfo sumArrayMethod1 = calculatorType1.GetMethod("SumArray");

            object[] arrayParams1 = new object[4];

            arrayParams1[0] = 5;
            arrayParams1[1] = 8;
            arrayParams1[2] = 2;
            arrayParams1[3] = 1;
            int sum1;
            //Call "SumArray" Method
            sum1 = (int)sumArrayMethod.Invoke(calculatorInstance, arrayParams1);


            Console.WriteLine("Sum = {0}", sum1);

            Console.ReadLine();

        }



    }
}

Class containing the 2 methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReflectionWithLateBinding
{
    public class Calculator
    {
        public int SumNumbers(int input1, int input2)
        {
            return input1 + input2;
        }


        public int SumArray(int[] input)
        {
            int sum = 0;
            for (int i = 0; i < input.Length; i++)
            {
                sum += i;
            }
            return sum;
        }        
    }


}

Solution

  • You're not passing an integer array to the reflected method, you're passing an object array with 4 integer values. This will cause reflection to want to find a method with 4 integer parameters. Instead, you want to pass the integer array as one of the values in your object array.

    Change to this:

    int[] numbers = { 5, 8, 2, 1 };
    object[] arrayParams1 = { numbers };
    

    I also want to point out that your Sum method isn't written correctly. You are just summing 0 to input.Length, and not the values present in the array.

    You want

    sum += input[i];
    

    Finally, depending on why you want to do this, using dynamic in C# would be easier than using reflection and would ultimately result in the roughly the same type of exception scenarios (method not found on the object you are invoking the methods on).

    dynamic calculatorInstance1 = Activator.CreateInstance(calculatorType1);
    calculatorInstance1.SumArray(new int[] { 5,8,2,1 });
    

    Edit, full working sample. The only thing I did was change your parameter array to my code above.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Reflection;
    
    namespace ReflectionWithLateBinding
    {
        public class Program
        {
            static void Main()
            {
                //load the current executing assembly
                Assembly executingAssembly1 = Assembly.GetExecutingAssembly();
    
                //load and instantiate the class dynamically at runtime - "Calculator class"
                Type calculatorType1 = executingAssembly1.GetType("ReflectionWithLateBinding.Calculator");
    
    
                //Create an instance of the type --"Calculator class"
                object calculatorInstance1 = Activator.CreateInstance(calculatorType1);
    
                //Get the info of the method to be executed in the class
                MethodInfo sumArrayMethod1 = calculatorType1.GetMethod("SumArray");
    
                int[] numbers = { 5, 8, 2, 1 };
                object[] arrayParams1 = { numbers };
    
                int sum1;
                //Call "SumArray" Method
                sum1 = (int)sumArrayMethod1.Invoke(calculatorInstance1, arrayParams1);
    
    
                Console.WriteLine("Sum = {0}", sum1);
    
                Console.ReadLine();
    
            }
        }
    
        public class Calculator
        {
            public int SumArray(int[] input)
            {
                int sum = 0;
                for (int i = 0; i < input.Length; i++)
                {
                    sum += input[i];
                }
                return sum;
            }
        }
    }