如何修复错误CS1738指定所有固定参数后,必须出现命名参数规范

时间:2019-07-16 16:52:18

标签: c#

public static Mutex CreateMutex(){
    MutexAccessRule rule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
    MutexSecurity mutexSecurity = new MutexSecurity();
    mutexSecurity.AddAccessRule(rule);
    bool createdNew;
    return new Mutex(initiallyOwned: false,"Global\\E475CED9-78C4-44FC-A2A2-45E515A2436", out createdNew,mutexSecurity);
}
  

错误CS1738指定所有固定参数后,必须出现命名参数规范

3 个答案:

答案 0 :(得分:0)

必须将命名参数(具有[name]:[value]格式的命名参数)放在没有指定名称的参数之后。我不知道构造函数的签名,但想法是将命名的args放在最后,例如:

public static Mutex CreateMutex() {
 MutexAccessRule rule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
 MutexSecurity mutexSecurity = new MutexSecurity();
 mutexSecurity.AddAccessRule(rule);
 bool createdNew;
 return new Mutex( "Global\E475CED9-78C4-44FC-A2A2-45E515A2436", out createdNew, mutexSecurity, initiallyOwned: false,);
}

答案 1 :(得分:0)

因此引用了C#文档

  

命名参数与位置参数一起使用时有效

     
      
  • 因为它们后面没有任何位置参数
  •   

这就是原因,为什么会遇到编译错误。

在使用C#7.2及更高版本时,它表示:

  

命名参数与位置参数一起使用时有效

     
      
  • 使用正确的位置。
  •   

有关更多信息,请参见: Named and Optional Arguments


所以,如果我们看一下constructor

public Mutex (bool initiallyOwned,
              string name,
              out bool createdNew,
              System.Security.AccessControl.MutexSecurity mutexSecurity);

在您的情况下,我们将看到职位处于正确的顺序。 而且,如果您要切换到C#7.2或更高版本,您的代码将会编译。

但是对于较低的版本,您有两个选择:

  1. 删除命名为initiallyOwned的参数
  2. 对所有参数使用命名参数

like:

return new Mutex(initiallyOwned: false,
                 name: "Global\E475CED9-78C4-44FC-A2A2-45E515A2436",
                 createdNew: out createdNew,
                 mutexSecurity: mutexSecurity); 

使用第二个选项时,已命名参数的位置不再重要。

答案 2 :(得分:0)

我最近也遇到了这个问题。受影响的代码是在CI中使用MsBuild 15(〜VS 2017)而不是MsBuild 16(〜VS 2019)进行编译的,开发人员又在本地使用了MsBuild 16。

使用相同的语言设置(由于框架4.7.2自动选择),这会导致CI中出现CS1738错误。

我在CI步骤中升级了用过的MsBuild,而不是更改代码。