如何在 C# 中组合委托
我想实现一个方法,该方法接受两个 Action A1 和 Action A2 委托并返回将它们组合在一起的新委托。该方法的签名如下:
public static Action<Tuple<T1,T2>> CombineWith<T1,T2>(this Action<T1> a1, Action<T2> a2)
因此,
{
A1(t1);
A2(t2);
}
我不想写:
{
A1.CombineWith(A2)(Tuple.Create(t1,t2));
}
该方法的可能实现是什么?
I want to implement a method which takes two Action A1 and Action A2 delegates and returns new delegate, which combines them. The signature of he method is the following:
public static Action<Tuple<T1,T2>> CombineWith<T1,T2>(this Action<T1> a1, Action<T2> a2)
So, instead of saying
{
A1(t1);
A2(t2);
}
I want to be able to write:
{
A1.CombineWith(A2)(Tuple.Create(t1,t2));
}
What is the possible implementation of this method can be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你想要:
用法:
编辑:顺便说一句,我注意到你在评论中提到“单独考虑参数会更可取”。在这种情况下,您可以执行以下操作:
I think you want:
Usage:
EDIT: By the way, I noticed you mentioned in a comment that "taking arguments individually would be even more preferable". In that case, you can do: