C# 包含有限状态机吗?
我最近读到了有关 boost::statechart
库(有限状态机)的内容,我喜欢这个概念。
C#有类似的机制吗?或者可以使用特定的设计模式来实现吗?
I've recently read about the boost::statechart
library (finite state machines) and I loved the concept.
Does C# have a similar mechanism ? Or can it be implemented using a specific design pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
.NET 4 Update 1 现在在以下类中支持它:
System.Activities.Statements.StateMachine
这里是有关如何使用它的教程。
这是动手实验。
.NET 4 Update 1 now supports it in the following class:
System.Activities.Statements.StateMachine
Here is a tutorial on how to use it.
Here's a hands on lab.
工作流基础 (.NET 3.0) 有一个状态机工作流程。 4.0 目前没有完全相同的东西,但是您绝对可以使用 4.0 创建状态机工作流程。
Workflow Foundation (.NET 3.0) has a state machine workflow. 4.0 doesn't have exactly the same thing currently, but you can definitely create a state machine workflow using 4.0.
我维护一个开源项目,它实现了(除其他外).NET 的通用有限状态机。它构建在 QuickGraph 之上,因此您可以免费获得许多图形分析算法。
有关该项目的更多信息,请参阅此页面,特别是“Jolt.Automata:有限状态机" 了解有关该功能的更多信息。
I maintain an open-source project which implements (among other things) a generic finite state machine for .NET. It is built on top of QuickGraph, so you get many graph-analysis algorithms for free.
See this page for more information about the project, and specifically "Jolt.Automata : Finite State Machines" for more information about the feature.
查看无状态 -> http://code.google.com/p/stateless/。它是较重的 WWF 的轻量级替代品。
以下是该工具作者的几篇文章:
域模型中的状态机
无状态中的参数化触发器和可重入状态
Check out Stateless -> http://code.google.com/p/stateless/. Its a lightweight alternative to the heavier WWF.
Here's a couple of articles by the author of the tool:
State Machines in Domain Models
Parameterised Triggers and Re-entrant States in Stateless
与 FSM 很接近的是 .NET 3.5 中的工作流,但是,工作流也不完全是 FSM。
使用 FSM 的强大之处在于,您可以在代码中显式创建它们,从而减少产生错误的机会。此外,当然有些系统本质上就是 FSM,因此这样编码更自然。
The things that come near to FSMs are workflows in .NET 3.5, however, also workflows are not exactly FSMs.
The power of using FSMs is that you can create them explicitly in your code, having less chance of creating bugs. Besides, of course some systems are FSMs by nature, so it is more natural to code them like so.
Windows Workflow Foundation (WF) 是 3.0 和 3.5 中基类库的一部分,包括状态机工作流设计,用于管理应用程序的状态机。
他们为即将发布的 4.0 版本完全重写了工作流程,并且新的 WF 4.0 类本身不支持状态机,但所有 3.0/3.5 类在 4.0 下仍然受到完全支持。
Windows Workflow Foundation (WF) that is part of the base class library in 3.0 and 3.5 includes a state-machine workflow design to manage state machines for your applications.
They have completely rewritten workflow for the upcoming 4.0 release, and the new WF 4.0 classes do not natively support state-machines, but all of the 3.0/3.5 classes are still fully supported under 4.0.
是的,C# 有迭代器块< /a> 是编译器生成的状态机。
如果您希望实现自己的状态机,可以创建
IEnumerable
和IEnumerator
接口的自定义实现。这两种方法都强调了 .NET 框架对迭代器模式的实现。
Yes, C# has iterator blocks which are compiler-generated state machines.
If you wish to implement you own state machine you can create custom implementations of the
IEnumerable<T>
andIEnumerator<T>
interfaces.Both of these approaches highlight the .NET framework's implementation of the iterator pattern.
此仓库中的其他替代方案 https://github.com/lingkodsoft/StateBliss
使用流畅的语法,支持触发器。
Other alternative in this repo https://github.com/lingkodsoft/StateBliss
used fluent syntax, supports triggers.