.net - StateMachineAttribute 在 .NET 中的用途是什么?
问题描述
我遇到StateMachineAttribute
了文档中调用的一个类。StateMachine
.NET中的用途是什么?用途是什么StateMachineAttribute
?
解决方案
当您实现异步方法时,C# 编译器使用从 StateMachineAttribute 派生的 AsyncStateMachineAttribute 生成方法
C# 代码
...
public async Task HelloWorldAsync()
{
await Console.Out.WriteLineAsync("Hello World!");
}
...
“真实”方法(由编译器生成)
[AsyncStateMachine(typeof(<HelloWorldAsync>d__4))]
[DebuggerStepThrough]
public Task HelloWorldAsync()
{
<HelloWorldAsync>d__4 stateMachine = new <HelloWorldAsync>d__4();
stateMachine.<>4__this = this;
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
编译器确实不使用 StateMachineAttribute。对于状态机方法,编译器将应用 AsyncStateMachineAttribute 或 IteratorStateMachineAttribute
推荐阅读
- assembly - 从 ARM 购买 IP 的半导体制造商是否应该满足参考手册中描述的指令的时钟周期?
- java - 使用 Google Maps Java API 时出现 java.net.UnknownServiceException
- angular - 如何从角度 7 中的当前 url 获取路由名称
- django - 如何在 sql 中添加几个字段的值并使用 Django 从该总和中创建一个?
- list - 从列表的双端队列中提取“列”
- lua - 如何或可以将带有参数的函数作为参数传递给lua中的函数?
- arrays - 通过从主阵列复制项目来创建第二个阵列
- markdown - 如何在 ProseMirror 状态下投影位置以转换为 Markdown 字符串
- asp.net-core - 尝试激活“MediatR.ServiceFactory”时无法解析“System.Object”类型的服务
- python-3.x - 使用 numpy linalg svd 时遇到问题