如何将整数数组传递给方法:Reflection C#

本文关键字:方法 Reflection 整数 数组 | 更新日期: 2023-09-27 18:24:05

我目前正在学习C#中的反射。我正在使用后期绑定从一个类中调用两个方法。第一种方法(SumNumbers)有效。第二个方法(SumArray)抛出一个异常,称为"参数计数不匹配"。有人能告诉我如何将整数数组传递给这个方法吗?

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();
        }

    }
}

包含两个方法的类

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

}

如何将整数数组传递给方法:Reflection C#

您不是在向反射方法传递一个整数数组,而是在传递一个具有4个整数值的对象数组。这将导致反射想要找到一个具有4个整数参数的方法。相反,您希望将整数数组作为对象数组中的值之一进行传递。

更改为:

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

我还想指出,你的求和方法写得不正确。你只是把0和输入相加。长度,而不是数组中存在的值。

你想要

sum += input[i];

最后,根据为什么要这样做,在C#中使用动态会比使用反射更容易,并且最终会导致大致相同类型的异常场景(在调用方法的对象上找不到方法)。

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

编辑,完整的工作样本。我所做的唯一一件事就是将您的参数数组更改为上面的代码。

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