【发布时间】:2022-01-27 00:19:29
【问题描述】:
我正在开发一个 .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}' 语句的重复数量。 我曾尝试使用 switch 映射来减少重复句子:
var switchMappings = new Dictionary<string, string>
{
"--Logins", "CommandArgs:Logins"
};
configBuilder.AddCommandLine(args, switchMappings);
实现类似的目标:
--Logins:0=Login0 --Logins:1=Login1
但它似乎不是那样工作的。
有没有办法在传递数组时使用开关映射或任何更简单的语法?
【问题讨论】:
标签: c# .net-core command-line-arguments asp.net-core-hosted-services