[英]C# Reflection Invoke - Object of Type 'XXX' Cannot Be Converted to type 'System.Object[]'
I have created an instance called input
that is of type: 我创建了一个名为
input
的实例,其类型为:
public class TestInput
{
public int TesTInt { get; set; }
}
I use this in this function: 我在此功能中使用此功能:
public static class TestClass
{
public static string TestFunction()
{
var testInput = new TestInput();
string res = ServicesManager.Execute<string>((object) testInput);
return res;
}
}
The Execute
function is here: Execute
功能在这里:
public static OUT Execute<OUT>(object input)
where OUT : class
{
var method = //getting method by reflection
object[] arr = new object[] { input };
return method.Invoke(null, arr) as OUT; //Error is triggered here
}
The method that I invoke is this one: 我调用的方法是这样的:
public static string TestFunctionProxy(object[] input)
{
var serviceInput = input[0] as TestInput;
//rest of code
}
I received the error in the title. 我收到标题错误。 (XXX - "TestInput" type)
(XXX-“ TestInput”类型)
What's happening and what is causing this error? 发生什么情况以及导致此错误的原因是什么?
Note: method
is static so no instance is required for the first parameter. 注意:
method
是静态的,因此第一个参数不需要实例。 Please correct me if I'm wrong. 如果我错了,请纠正我。
Any help is appreciated. 任何帮助表示赞赏。
EDIT: Updated the question with some more code for a complete example. 编辑:使用更多代码更新了问题,以获取完整的示例。
You are passing the wrong arguments to the method. 您将错误的参数传递给该方法。 It wants an object[] and you are giving a simpe object.
它需要一个object [],而您要提供一个simpe对象。 This is how to fix it:
这是解决方法:
object[] arr = new object[] { new object[] { input } };
The 'outer' object[] is the parameter for Invoke, the 'inner' array is the parameter for your method. “外部”对象[]是Invoke的参数,“内部”数组是您的方法的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.