我有一些需要創建並添加到數組的對象。但是,從長遠來看,下面的代碼看起來很髒並且很難維護。我在想的是,我應該將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