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

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

我目前正在学习 C# 中的反射。我正在使用后期绑定从 class 调用 2 个方法。第一种方法 (SumNumbers) 有效。第二种方法 (SumArray) 抛出异常 "Parameter count mismatch"。谁能告诉我如何将整数数组传递给此方法?

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 包含 2 个方法

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


}

您没有将整数数组传递给反射方法,而是传递了一个包含 4 个整数值的对象数组。这会导致反射想要找到一个有4个整型参数的方法。相反,您想将整数数组作为对象数组中的值之一传递。

改为:

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

我还想指出您的 Sum 方法写得不正确。您只是将 0 加到 input.Length,而不是数组中存在的值。

你想要

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