mardi 24 novembre 2015

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 executingAssembly = Assembly.GetExecutingAssembly();

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


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

            //Get the info of the method to be executed in the class
            MethodInfo sumArrayMethod = calculatorType.GetMethod("SumNumbers");

            object[] arrayParams = new object[2];

            arrayParams[0] = 5;
            arrayParams[1] = 8;
            int sum;
            //Call "SumNumbers" Method
            sum = (int)sumArrayMethod.Invoke(calculatorInstance, arrayParams);

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



            //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;
        }        
    }


}





Aucun commentaire:

Enregistrer un commentaire