c# - 签名中带有 params string[] 的重载函数

标签 c# .net c#-4.0

我有这两个功能

    public static string Function1 (string id, params string[])
    {
       return Function1(id, null, null, params)
    }

public static string Function1 (string id, string id2, Object a, params string[])
{
      string id = id,
      if (IsValidId(id))
      {
            start = new ProcessStartInfo();
            start.Arguments = params;
            if (string.IsNullOrEmpty(id2)==false)
            {
                start.RedirectStandardOutput = true;
            }
      }
}

我想在执行以下调用时使用第二个重载

MyStaticClaass.Function1(
        input1,
        input2,
        null, // (This one represents the Object)
        input3,
        input4,
        input5);

有没有办法强制它转到方法的第二个定义?

我遇到的编译错误是:此调用在以下方法或属性之间不明确:(然后是上面的两个方法)

PS:这两个功能我都没有选择。我无法更改他们的签名或姓名。

最佳答案

您可以使用命名参数来重载特定函数,例如

 Program.Function1(
        id: input1,
        id2: input2,
        o:null,
        array: new string[] {input3,
            input4,
            input5});

它会命中函数

public static string Function1 (string id, string id2, Object o, params string[] array)
{

}

有关更多详细信息,您可以查看此 Named Arguments

关于c# - 签名中带有 params string[] 的重载函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777580/

相关文章:

c# - 使用 LINQ 检索数据

c# - 网格控制

c# - 最小起订量 - 如何验证静态类调用和委托(delegate)?

c# - 当实现接口(interface)的类有自己的属性时,如何使用接口(interface)?

css - 如何在 ASP.NET Web 窗体中渲染 CSS 内联

c# - 如何正确地将 DataReader 转换为 DTO/List<DTO>?

c# - 线程在 2 分钟后什么都不做

c# - MSChart 如何更改一系列破折号之间的间距

C# FileInfo - 查找重复文件

c# - 从 Python 调用用 C# 编写的函数时,“NoneType”对象不可调用