I wanted to do something ‘dynamic’ with a dictionary object and supplied command line arguments. The command line arguments are Boolean and I could then call methods if any one of them was true. So…
public class CommandLineArguments
{
public bool AddSection1 { get; set; }
public bool AddSection2 { get; set; }
public bool Addsection3 { get; set; }
}
class RunSomeActions
{
private Dictionary<bool, Action> methodList = new Dictionary<bool, Action>();
public RunSomeActions()
{
// create the switches as if from a command line
CommandLineArguments parameters = new CommandLineArguments();
parameters.AddSection1 = true;
parameters.AddSection2 = false;
parameters.Addsection3 = true;
// setup the methods for the switches
methodList.Add(parameters.AddSection1, this.Section1);
methodList.Add(parameters.AddSection2, this.Section2);
methodList.Add(parameters.Addsection3, this.Section3);
foreach (var entry in methodList)
{
// if the switch is on
// call the method
if (entry.Key)
methodList[entry.Key]();
}
}
private void Section1()
{
// add specific entries into a file
}
private void Section2()
{
// perform analysis on a file
}
private void Section3()
{
// delete everything and start again
}
}
This works great if you only ever have two values of true and false, so it’s not much good really. What I did like about this approach was not having to parse the arguments manually and then build an Action list. Is there a way I can salvage this design?
Since you’re not actually using the dictionary for lookups, but just for storage and iteration, instead of using a
Dictionary<K,V>, you can just use aList<KeyValuePair<K,V>>.The main difference from a code perspective would be changing .Add to:
Then, when you use, switch to:
That being said, you can take this one step further, potentially, and just store a
List<Action>directly. Only add actions to the list where the condition is true, then execute them all.