Can you chain the result of one delegate to be the input of another in C#?(您可以将一个委托的结果链接到 C# 中另一个委托的输入吗?)
问题描述
我正在寻找一种方法来链接多个委托,以便一个委托的结果成为下一个委托的输入.我试图在方程求解程序中使用它,其中部分是通过不同的方法完成的.这个想法是,当您构建方程式时,程序会添加代表并以特定顺序将它们链接起来,因此可以正确求解.如果有更好的方法来解决这个问题,请分享.
I am looking for a way to chain several delegates so the result from one becomes the input of the next. I am trying to use this in equation solving program where portions are done by different methods. The idea is that when you are building the equation the program adds the delegates and chains them in a particular order, so it can be solved properly. If there is a better way to approach the problem please share.
推荐答案
这可能会有所帮助:
public static Func<T1, TResult> Compose<T1, T2, TResult>(Func<T1, T2> innerFunc, Func<T2, TResult> outerFunc) {
return arg => outerFunc(innerFunc(arg));
}
这执行 函数组合,运行 innerFunc 并传递结果提供初始参数时到 outerFunc:
This performs function composition, running innerFunc and passing the result to outerFunc when the initial argument is supplied:
Func<double, double> floor = Math.Floor;
Func<double, int> convertToInt = Convert.ToInt32;
Func<double, int> floorAndConvertToInt = Compose(floor, convertToInt);
int result = floorAndConvertToInt(5.62);
Func<double, int> floorThenConvertThenAddTen = Compose(floorAndConvertToInt, i => i + 10);
int result2 = floorThenConvertThenAddTen(64.142);
这篇关于您可以将一个委托的结果链接到 C# 中另一个委托的输入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:您可以将一个委托的结果链接到 C# 中另一个委托的输入吗?
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
