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);
}
Error CS1738 Named argument specifications must appear after all fixed arguments have been specified
最佳答案
所以引用 C# Doc
Named arguments, when used with positional arguments, are valid as long
- as they're not followed by any positional arguments
所以这就是原因,为什么你会遇到编译错误。
使用 C# 7.2 及更高版本时,它说:
Named arguments, when used with positional arguments, are valid as long
- as they're used in the correct position.
有关更多信息,请参阅:
Named and Optional Arguments
所以如果我们看一下 constructor :
public Mutex (bool initiallyOwned,
string name,
out bool createdNew,
System.Security.AccessControl.MutexSecurity mutexSecurity);
我们将看到在您的情况下,位置的顺序是正确的。
如果您要切换到 C# 7.2 或更高版本,您的代码将被编译。
但是对于较低版本,您有两种选择:
initiallyOwned
的参数命名喜欢:
return new Mutex(initiallyOwned: false,
name: "Global\E475CED9-78C4-44FC-A2A2-45E515A2436",
createdNew: out createdNew,
mutexSecurity: mutexSecurity);
使用第二个选项时,命名参数的位置不再重要。
关于c# - 如何修复错误 CS1738 命名参数规范必须在指定所有固定参数后出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57062106/