我使用ManyConsole作为控制台应用程序的命令行命令和选项解析器。所有命令都定义为从ConsoleCommand
派生的命令类,然后执行特定的任务。我定义了一个中介的基类,ParkPayConsoleCommand
从该类派生的:为什么ManyConsole显示一个CommandLine类的公共成员?
abstract class ParkPayConsoleCommand: ConsoleCommand
{
protected readonly ParkPayDbContext DbContext = new ParkPayDbContext();
}
然后我所有的命令类从我的基类派生,并享受内置DbContext
,如:
class ReadStartsCommand : ParkPayConsoleCommand
{
public ReadStartsCommand()
{
_commandTrace = new TraceSource(CommandName, SourceLevels.All);
IsCommand("read-starts", "Processes imported vehicle entry movements to create new VehiclePresence records with start date-times based on those movements");
HasRequiredOption("b|batchId:", "The Id of the VehicleMovementBatch used to create new vehicle presences.", b => _batchIdOption = b);
}
public override int Run(string[] remainingArguments)
{
// Do the business of the command.
return (int)ExitCodes.Success;
}
}
这是一个ManyConsole约定为每个命令类命名和描述自己,并在其构造函数中定义其命令行选项,如上所示。正常情况下,当我运行上面的命令(如ReadStartsCommand
)时,它只是写入控制台正在运行的命令,而不是我传递的选项。
然而,当我做ParkPayConsoleCommand.DbContext
公开,没有保护,
的DbContext输出字符串:ParkPay.Model.Context.ParkPayDbContext
到控制台在运行命令的名称结束和说明。为什么当DbContext
没有被任何地方定义为命令选项本身时,它会这样做。这可能看起来微不足道,但本质上我提出了一个非常重要的'元问题',那就是:ManyConsole是否隐式地将其命令类的所有公共属性解释为命令选项,即使它们没有被明确声明为这样?