I have a state machine that needs to call a different method on each object from a List of objects depending on the state I'm in. Basically I'm trying to refactor the code that has a loop in each case statement of my state machine so that it looks like the code below. However I cannot seem to figure out how to pass the relevant method to my refactored function (not to mention I then don't know how to call it on each item)
Any help would be appreciated.
Here's the example code:
public class MyOtherType { public bool Method1() { return false; } public bool Method2() { return false; } public bool Method3() { return false; } public bool Method4() { return false; } } public class MyType { public enum MyState { DoSomething1, DoSomething2, DoSomething3, DoSomething4 } private MyState State = MyState.DoSomething1; List<MyOtherType> MyListOfObjects = new List<MyOtherType>() { new MyOtherType(), new MyOtherType() }; private void StateMachine() { switch (State) { case MyState.DoSomething1: //How do I pass this in? Do I need to set it up differnetly? Process(() => MyOtherType.Method1()); break; case MyState.DoSomething2: Process(() => MyOtherType.Method2); break; case MyState.DoSomething3: Process(() => MyOtherType.Method3); break; case MyState.DoSomething4: Process(() => MyOtherType.Method4); break; } } private void Process(Func<bool> method) { foreach (MyOtherType item in MyListOfObjects) { //How do I call the method on each item? if (item.method()) { //Do something } } } }
Func<T>in my opinion.