How do I build an Action action in a loop? to explain (sorry it's so lengthy)
I have the following:
public interface ISomeInterface { void MethodOne(); void MethodTwo(string folder); } public class SomeFinder : ISomeInterface { // elided }
and a class which uses the above:
public Map Builder.BuildMap(Action<ISomeInterface> action, string usedByISomeInterfaceMethods) { var finder = new SomeFinder(); action(finder); }
I can call it with either of these and it works great:
var builder = new Builder(); var map = builder.BuildMap(z => z.MethodOne(), "IAnInterfaceName"); var map2 = builder(z => { z.MethodOne(); z.MethodTwo("relativeFolderName"); }, "IAnotherInterfaceName");
How can I build the second implementation programmatically? i.e.,
List<string> folders = new { "folder1", "folder2", "folder3" }; folders.ForEach(folder => { /* do something here to add current folder to an expression so that at the end I end up with a single object that would look like: builder.BuildMap(z => { z.MethodTwo("folder1"); z.MethodTwo("folder2"); z.MethodTwo("folder3"); }, "IYetAnotherInterfaceName"); */ });
I've been thinking I need an
Expression<Action<ISomeInterface>> x
or something similar, but for the life of me, I'm not seeing how to construct what I want. Any thoughts would be greatly appreciated!
Action is a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.
You can use the Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate.
Well, Action<T> is generic, meaning that various kinds of Action<T> can take parameters of different types. For example, an Action<string> can accept a string parameter. void ShowMessage(string message) { Console.WriteLine(message); } Action<string> x = ShowMessage; x("Hello world"); //Displays "Hello world"
It's really easy, because delegates are already multicast:
Action<ISomeInterface> action1 = z => z.MethodOne(); Action<ISomeInterface> action2 = z => z.MethodTwo("relativeFolderName"); builder.BuildMap(action1 + action2, "IAnotherInterfaceName");
Or if you've got a collection of them for some reason:
IEnumerable<Action<ISomeInterface>> actions = GetActions(); Action<ISomeInterface> action = null; foreach (Action<ISomeInterface> singleAction in actions) { action += singleAction; }
Or even:
IEnumerable<Action<ISomeInterface>> actions = GetActions(); Action<ISomeInterface> action = (Action<ISomeInterface>) Delegate.Combine(actions.ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With