代码之家  ›  专栏  ›  技术社区  ›  klas mack

为什么以参数形式调用带有params object[]的方法会引发强制转换错误?

  •  0
  • klas mack  · 技术社区  · 6 年前

    在我发送的客户机上尝试调用方法时,当使用参数时会引发此错误:

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Steve Harris    6 年前

    CallExternalSoap<string[]>(...