我创建了一个叫做input
实例,它是类型:C#反射调用 - 类型“XXX”的对象不能转换为类型“System.Object的[]”
public class TestInput
{
public int TesTInt { get; set; }
}
我在这个函数中使用:
public static class TestClass
{
public static string TestFunction()
{
var testInput = new TestInput();
string res = ServicesManager.Execute<string>((object) testInput);
return res;
}
}
的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
}
,我调用是THI的方法s:
public static string TestFunctionProxy(object[] input)
{
var serviceInput = input[0] as TestInput;
//rest of code
}
我在标题中收到错误。 (XXX - “TestInput”类型)
发生了什么以及是什么导致了此错误?
注意:method
是静态的,因此第一个参数不需要实例。如果我错了,请纠正我。
任何帮助表示赞赏。
编辑:用一些更多的代码更新了问题的完整例子。
如果您提供了一个简短的*完整的*示例,那将会更容易帮助您。我也怀疑错误消息中没有包含“XXX”。我们不知道你在哪里得到错误... –
@JonSkeet不够公平,会更新问题。 –
将对象作为对象并将其转换为对象的对象是两件不同的事情。您的方法期望获得哪些*精确*参数类型? – Crono