我如何组合多个动作<;T>;变成一个动作<;T>;在C#中?

我如何组合多个动作<;T>;变成一个动作<;T>;在C#中?,c#,lambda,C#,Lambda,如何在循环中构建动作?解释(抱歉,太长了) 我有以下资料: public interface ISomeInterface { void MethodOne(); void MethodTwo(string folder); } public class SomeFinder : ISomeInterface { // elided } 以及使用上述内容的类: public Map Builder.BuildMap(Action<ISomeInterface>

如何在循环中构建动作?解释(抱歉,太长了)

我有以下资料:

public interface ISomeInterface {
    void MethodOne();
    void MethodTwo(string folder);
}

public class SomeFinder : ISomeInterface 
{ // elided 
} 
以及使用上述内容的类:

public Map Builder.BuildMap(Action<ISomeInterface> action, 
                            string usedByISomeInterfaceMethods) 
{
    var finder = new SomeFinder();
    action(finder);
}
如何以编程方式构建第二个实现?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");
                */
                });
List folders=new{“folder1”、“folder2”、“folder3”};
folders.ForEach(folder=>
{
/*在此处执行某些操作以将当前文件夹添加到表达式
最后我得到了一个单独的物体
看起来像:
BuildMap(z=>{
z、 方法二(“folder1”);
z、 方法二(“folder2”);
z、 方法二(“folder3”);
},“IYetAnotherInterfaceName”);
*/
});
我一直在想我需要一个

Expression<Action<ISomeInterface>> x 
表达式x

或者类似的东西,但对于我来说,我不知道如何构建我想要的东西。任何想法都将不胜感激

这真的很容易,因为代理已经是多播的:

Action<ISomeInterface> action1 = z => z.MethodOne();
Action<ISomeInterface> action2 = z => z.MethodTwo("relativeFolderName");
builder.BuildMap(action1 + action2, "IAnotherInterfaceName");
Action action1=z=>z.MethodOne();
Action action2=z=>z.MethodTwo(“relativeFolderName”);
BuildMap(action1+action2,“iOnotherInterfaceName”);
或者,如果你因为某种原因收集了一批:

IEnumerable<Action<ISomeInterface>> actions = GetActions();
Action<ISomeInterface> action = null;
foreach (Action<ISomeInterface> singleAction in actions)
{
    action += singleAction;
}
IEnumerable actions=GetActions();
Action=null;
foreach(动作中的单动作)
{
动作+=单动作;
}
甚至:

IEnumerable<Action<ISomeInterface>> actions = GetActions();
Action<ISomeInterface> action = (Action<ISomeInterface>)
    Delegate.Combine(actions.ToArray());
IEnumerable actions=GetActions();
动作动作=(动作)
Delegate.Combine(actions.ToArray());

感谢您的快速响应!我现在正在尝试这个,但到目前为止它看起来不错。这就成功了,谢谢!这是一个很好的提醒,首先要考虑简单的解决方案!这太美了。直到刚才我才真正理解多播代理的作用。
IEnumerable<Action<ISomeInterface>> actions = GetActions();
Action<ISomeInterface> action = (Action<ISomeInterface>)
    Delegate.Combine(actions.ToArray());