[英]How can I pass an integer array to a method: Reflection C#
I am currently learning about Reflection in C#. 我目前正在学习有关C#中的Reflection的知识。 I am calling 2 methods from a class using late binding.
我正在使用后期绑定从类中调用2个方法。 The first method (SumNumbers) works.
第一种方法(SumNumbers)有效。 The second method (SumArray) throws an exception saying "Parameter count mismatch".
第二种方法(SumArray)引发异常,指出“参数计数不匹配”。 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 包含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;
}
}
}
You're not passing an integer array to the reflected method, you're passing an object array with 4 integer values. 您没有将整数数组传递给反射方法,而是传递了具有4个整数值的对象数组。 This will cause reflection to want to find a method with 4 integer parameters.
这将导致反射想找到具有4个整数参数的方法。 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. 我还想指出,您的Sum方法编写不正确。 You are just summing 0 to input.Length, and not the values present in the array.
您只是将0与input.Length相加,而不是数组中存在的值。
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). 最后,这取决于你为什么要做到这一点,使用C#动态会比使用反射更容易,最终会导致大致相同类型的例外情形(方法不是要调用的方法的对象上找到)。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.