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

标签 c# reflection late-binding

我目前正在学习 C# 中的反射。我正在使用后期绑定(bind)从一个类中调用 2 个方法。第一种方法 (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();

        }



    }
}

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

关于c# - 如何将整数数组传递给方法 : Reflection C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33901047/

相关文章:

reflection - 如何通过名称获取对 Kotlin 对象的引用?

java - 从 JPA 获取 @Id 的 DataType

python - 词法闭包是如何工作的?

python - Tkinter。使用 "different"命令函数创建多个按钮

c++ - 使用派生类构造函数初始化对象

c# - 从 MySql 获取数据到 C# 应用程序

c# - C# 中如何检查日期是否已过?

java - 如何在运行时从字符串表示创建 Java 对象

c# - 如何以正确的方式处理 BackgroundWorkers

c# - 基于示例的关于继承的混淆