我需要创建一些对象并添加到数组中。但是,从长远来看,下面的代码看起来很脏并且难以维护。我在想的是,我应该将Name和Value属性存储在一个表中,并在运行时构建每个comCommand对象。
但是,我不确定这样做的最佳方法是什么... Reflection,Activator.CreateInstance或某种对象工厂?
提前致谢。
var engine = new comCommand() { commandName = "-e", commandValue = "PNetTNative" };
var outputFile = new comCommand() { commandName = "-f", commandValue = OutputFile };
var groupSize = new comCommand() { commandName = "-GroupSizeParamInput1ParamsIn", commandValue = GroupSize };
var pagesPerSheet = new comCommand() { commandName = "-PagesPerSheetParamInput1ParamsIn", commandValue = PagesPerSheet };
var outputFileName = new comCommand { commandName = "-OutputFileNameParamInput1ParamsIn", commandValue = OutputFileName };
var duplex = new comCommand { commandName = "-DuplexParamInput1ParamsIn", commandValue = Duplex };
var processId = new comCommand { commandName = "-ProcessIDParamInput1ParamsIn", commandValue = ProcessID };
var request = new comRunWorkFlowReq();
request.command = new[] { engine, outputFile, groupSize, pagesPerSheet, outputFileName, duplex, processId };
答案 0 :(得分:1)
创建一个命令构造函数(正如Kirk建议的那样)并保持原样:多个comCommand(“ - e”,“PNetTNative”)等调用。
将其保存在代码中的原因是您获得了编译时类型和错误检查...是的,您可以在运行时(各种方法)执行此操作,但对于仅7个声明,最好将其保留在编译时。