2011-06-22 51 views
3

我有一个Func<Foo, object>Action<object>并希望这些组合成Action<Foo>,它结合了我的Func键和行动统一到其中的函数功能的结果传递给操作一个动作。有没有一个简单的方法来做到这一点?结合Func键<Foo, object>和行动<object>

回答

6

我能想到的最普遍的方法是这样的:

Action<T1> Combine<T1, T2>(Func<T1, T2> func, Action<T2> action) 
{ 
    return x => action(func(x)); 
} 

用法:

Func<Foo, object> func = x => x; 
Action<object> action = Console.WriteLine; 

Action<Foo> result = Combine(func, action); 

result(new Foo()); 
相关问题