序列化对函数的调用
我需要在我的应用程序中实现任务的概念。 在我的项目中,任务是一项必须执行的操作,并由名称标识:每个任务还具有输入参数(类型和值)并产生一个或多个输出。
必须实现 Task 类,我如何表达“具有某种结果的操作”的概念?我可以将操作的名称存储在字符串属性中,但是如何表达该操作返回的类型? 同样,当这些参数也可能是不同类型时,如何实现操作的输入参数列表?
目标是序列化任务对象并将其发送到另一个节点。接收者分析接收到的Task对象,读取操作的名称并执行相应的函数。
例如,假设节点 A 和节点 B “拥有”以下方法...
节点 A 的服务:
- double getAreaOfRectangle(double length, double width);
- double getAreaOfTriangle(double base, double height);
- double getAreaOfParallelogram(double base, double height).
节点 B 的服务:
- double getAreaOfCircle(double radius);
- double getAreaOfTrapezoid(double base1, double base2, double height).
如果节点 C 需要计算矩形的面积,它知道它必须联系节点 A并通过指定操作 (double areaOfRectangle) 和输入 (double width 和 double height) 向节点 A 发送消息。 同样,如果节点C需要计算圆的面积,它必须向节点B发送消息,指定操作和半径长度。 为了发送输入,因为它们可能是不同的类型(甚至是新的用户定义类型),我可以使用对象数组...
某些任务可能依赖于其他任务,即,因为输入参数可能至少有另一个任务的一个输出,因此我还需要实现一个任务的输出与另一个任务的输入之间的链接。
我应该如何继续定义类?
I need to implement the concept of task in my application.
In my project, the task is an operation that must be performed and is identified by a name: each task also has input parameters (types and values) and produces one or more outputs.
Having to implement the Task class, how can I express the concept of "operation that has some kind of result"? I could store the name of the operation in a string attribute, but how do I express the type returned by this operation?
Similarly, how can I implement a list of input parameters of the operation, when these parameters may also be of different types?
The goal is to serialize the Task object and send it to another node. The receiver analyzes the received Task object, reads the name of the operation and executes the corresponding function.
For example, suppose that node A and node B "have" the following methods...
Service of node A:
- double getAreaOfRectangle(double length, double width);
- double getAreaOfTriangle(double base, double height);
- double getAreaOfParallelogram(double base, double height).
Service of node B:
- double getAreaOfCircle(double radius);
- double getAreaOfTrapezoid(double base1, double base2, double height).
If the node C needs to calculate the area of a rectangle, it knows it must contact the node A and send a message to node A by specifying the operation (double areaOfRectangle) and the inputs (double width and double height).
Similarly, if the node C needs to calculate the area of a circle, it must send a message to node B, specifying the operation and the length of the radius.
In order to send the inputs, since they could be different types (or even new user-defined types), I could use an array of object...
Some tasks may depend on other tasks, ie, as input parameters may have at least one output of another task, and so I would need to implement also the link between the output of a task and the input of another.
How should I proceed to define the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该考虑在 .NET 4.0 中使用表达式树。有关详细信息,请参阅本文
在 C# 中序列化和反序列化表达式树
You should consider using Expression Trees in .NET 4.0. See this article for more info
Serializing and Deserializing Expression Trees in C#