我使用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是否隱式地將其命令類的所有公共屬性解釋爲命令選項,即使它們沒有被明確聲明爲這樣?