开发者

Building objects at runtime

I have a number of objects that I need to create and add to an array. However the code below seems dirty and difficult to maintain in the long run. What I'm thinking is, I should store the Name and Value properties in a table and build each comCommand object at runtime.

However, I'm not exactly sure what the best method to about doing this... Reflection, Activator.CreateInstance or some kind of object factory?

Thanks in advance.

 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 { commandNa开发者_如何学运维me = "-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 };


Create a command constructor (as Kirk suggested) and keep it as you have: multiple comCommand("-e","PNetTNative") etc calls.

The reason for keeping it in code is you get compiler-time type and error checking... Yes, you can do it at runtime (various methods) but for just 7 declarations, it's best to keep it at compile time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜