我正在开发一个.net通用主机应用程序,我希望通过命令行传递一些配置,然后将其绑定到一个对象:
public class CommandArgs
{
public string Mode { get; set; }
public string[] Logins { get; set; }
}此时,我知道可以像这样传递Logins数组:
--CommandArgs:Logins:0=Login0 --CommandArgs:Logins:1=Login1 ...问题是“--CommandArgs:Logins:{index}”语句的重复数。我尝试使用开关映射来减少重复句子:
var switchMappings = new Dictionary<string, string>
{
"--Logins", "CommandArgs:Logins"
};
configBuilder.AddCommandLine(args, switchMappings);达到以下目的:
--Logins:0=Login0 --Logins:1=Login1但似乎不是这样的。
在传递数组时是否有使用开关映射或任何更简单的语法的方法?
发布于 2021-12-28 10:40:02
您可以在不使用switchMappings的情况下完成它,并像这样更改args:
//--Logins:0 Login0 --Logins:1 Login1
configBuilder.AddCommandLine(args);
var config=configBuilder.Build();
var logins= config.GetSection("Logins").GetChildren().Select(x => x.Value).ToArray();测试并运行良好。
https://stackoverflow.com/questions/70505561
复制相似问题