I wrote a method that returns
List<KeyValuePair<CommandType, List<string>>>
CommandType is of type enum
public enum CommandType { Programmed, Manual }
My problem is that the value in KeyValuePair sometimes enum , and sometimes it is a list of strings, but I need to keep all KeyValuePair in one list.
I am currently passing the value as an object to keyvaluepair, and when the method returns the list and repeats it, based on the key, I return the value back to its original type.
Is there a better way to implement this?
here is a sample code
public enum ProgrammedCommands { Sntp, Snmp, } private List<KeyValuePair<CommandType, object>> GetCommandsFromTemplate(string[] templateLines) { var list = new List<KeyValuePair<CommandType, object>>(); if (templateLines != null) for (int lineIndex = 0; lineIndex < templateLines.Length; lineIndex++) { if (templateLines[lineIndex].Contains("!*") && templateLines[lineIndex].Contains("*!")) { KeyValuePair<CommandType, object> ProgrammedSetting; List<string> programmedCommandList; if (templateLines[lineIndex].Contains("SNTP - SNTP Server Commands")) { ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Programmed, ProgrammedCommands.Sntp); list.Add(ProgrammedSetting); } else if (templateLines[lineIndex].Contains("MANUAL")) { lineIndex++; List<string> manual = new List<string>(); while (true) { if (lineIndex >= templateLines.Length) break; if (templateLines[lineIndex].Contains("!![")) lineIndex++; else if (templateLines[lineIndex].Contains("]!!")) break; else { manual.Add(templateLines[lineIndex]); lineIndex++; } } ProgrammedSetting = new KeyValuePair<CommandType, object>(CommandType.Manual, manual); list.Add(ProgrammedSetting); } } } return list; }
source share