我正在为我的控制台应用程序制作一个命令系统。有些命令使用"as-is“(如"exitapp"),有些命令需要参数(如”服务器启动ip端口“)。我就是这样做的:
delegate void CommandHandlerMethod(params object[] args);
readonly Dictionary<string, CommandHandlerMethod> commands;
对于不需要参数的方法,我使用这样的丢弃特性:
void ExitAppCommand(params object[] _) { ... }
void StartServerCommand(params object[] args)
{
// ip = args[0];
// port = args[1];
}
然后,我循环遍历字典,我想在以下两种情况中做出决定:
那么,检测参数丢弃的选项是什么呢?
UDP:这是我真正想要的“伪”:
foreach (KeyValuePair<string, CommandHandlerMethod> cmd in commands)
{
(input.StartsWith(cmd.Key))
{
if (cmd.Value. /*is declared to discard arg */)
cmd.Value();
else
// else cut args and pass them
cmd.Value(input.Substring(cmd.Key.Length+1).Split(' '));
break;
}
}
https://stackoverflow.com/questions/57481579
复制相似问题