我有一些需要创建并添加到数组的对象。但是,从长远来看,下面的代码看起来很脏并且很难维护。我在想的是,我应该将Name和Value属性存储在一个表中,并在运行时构建每个comCommand对象。在运行时构建对象
但是,我不完全确定要做到这一点的最佳方法...反射,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 };
如果你只是声明了一个合适的构造函数,那么你就可以做'new comCommand(“ - e”,“PNetTNative”)'。另外,小写的类名完全是非标准的。真的应该是'ComCommand'。 – 2010-10-23 22:12:51
感谢您的建议。我同意你的意见,但ComCommand课程并未由我执行。 – 2010-10-23 23:14:39