[英]Why does invoking a method with params object[] as args throw a cast error?
I am trying to wrap my soap-calls with a generic wrapper to simplify logging and other commons. 我正在尝试用通用包装器包装我的电话会议,以简化日志记录和其他公用程序。
When trying to invoke a method on the client I send in, I get this error thrown when using parameters: 尝试在我发送的客户端上调用方法时,使用参数时出现此错误:
{"Unable to cast object of type 'System.String' to type 'System.String[]'."}
{“无法将类型为“ System.String”的对象转换为类型为“ System.String []”。”}
My code: 我的代码:
private T CallExternalSoap<T>(object client, string funcName, params object[] args)
{
var type = client.GetType();
var method = type.GetMethod(funcName);
if (method is null)
{
throw new NullReferenceException($"Could not find method {funcName} on object of type {type}.");
}
if (method.GetParameters().Length != args.Length)
{
throw new Exception($"Number of parameters in {args} does not match number of expected parameters in method {funcName} . Expected {method.GetParameters().Length} parameters");
}
var result = (T)method.Invoke(client, args);
return result;
}
No matter which objects/params I send in I get that error. 无论我发送哪个对象/参数,都会收到该错误。 Any ideas?
有任何想法吗?
The issue is not with the parameters but with casting the result. 问题不在于参数,而在于转换结果。
The SOAP method is returning a string but you are casting as a string[] based on the type parameter. SOAP方法返回一个字符串,但是您将根据类型参数将其转换为string []。
CallExternalSoap<string[]>(...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.