Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I combine several Action<T> into a single Action<T> in C#?

Tags:

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!

like image 590
JohnKeller Avatar asked Apr 01 '10 10:04

JohnKeller


People also ask

What is Action <> C#?

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.

What is action t used for in an application?

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.

What is Action T?

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"


1 Answers

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()); 
like image 136
Jon Skeet Avatar answered Sep 25 '22 07:09

Jon Skeet