c# - 在 .NET Standard 上实现异步返回类型
问题描述
大家好,我正在尝试使用.NetCore 框架创建我自己的Task
-Like 类型。
到目前为止在 . 我的自定义作品,它可以,我可以返回它而不是.
但是,该属性不存在,并且我尝试实现它:我用该属性装饰了我的等待类型,但它仍然不允许我使用它:AsyncMethodBuilderAttribute
Net Core
Type
awaited
async [MyTypeName]<T> MyMethod()
Task
.NetStandard 2.0
"The return type of an async method must be void,Task,Task<T>"
到目前为止,我可以在 . Net Core
它有效!
public async Task WrapperMethod(){
int result=await GetIntAsync();
}
public async Errand<int> GetIntAsync()
{
await Task.Delay(1000);
return 3;
}
属性:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false, AllowMultiple = false)]
public sealed class AsyncMethodBuilderAttribute : Attribute {
//
// Parameters:
// builderType:
public AsyncMethodBuilderAttribute(Type builderType) {
this.BuilderType = builderType;
}
//
public Type BuilderType { get; }
}
下面是自定义类型的实现和async method builder
.Theawaiter
在这种情况下并不重要,但如果需要,我可以提供源代码。
我Task
喜欢的类型
[AsyncMethodBuilder(typeof(Builder))]
public partial class Errand {
private readonly Builder mbuilder;
public Errand() {
}
internal Errand(Builder methodBuilder) => this.mbuilder = methodBuilder;
private readonly HashSet<Awaiter> awaiters = new HashSet<Awaiter>();
protected bool isCompleted = false;
public Awaiter GetAwaiter() {
Awaiter result = new Awaiter(this);
this.awaiters.Add(result);
return result;
}
}
自定义AsyncMethodBuilder
实现:
public partial class Builder {
#region " Compiler integration "
public static Builder Create() {
return new Builder();
}
protected IAsyncStateMachine myStateMachine;
public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine {
this.myStateMachine = stateMachine;
this.errand = new Errand(this);
stateMachine.MoveNext();
}
public void SetException(Exception ex) {
}
public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine machine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine {
var foo = machine as IAsyncStateMachine;
awaiter.OnCompleted(() => { foo.MoveNext(); });
}
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine machine)where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine {
IAsyncStateMachine asyncMachine = machine as IAsyncStateMachine;
awaiter.OnCompleted(() => asyncMachine.MoveNext());
}
private Errand errand;
public Errand Task { get { return this.errand; } }
public void SetStateMachine(IAsyncStateMachine stateMachine) {
}
public void SetResult() {
}
#endregion
// internal void AddAwaiter(Awaiter awaiter) => this.awaiters.Add(awaiter);
}
我还应该添加什么使其在.Net Standard 中工作?
解决方案
属性的源文件在哪里并不重要,但它确实需要具有正确的命名空间声明:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false, AllowMultiple = false)]
public sealed class AsyncMethodBuilderAttribute : Attribute
{
public AsyncMethodBuilderAttribute(Type builderType) =>
BuilderType = builderType;
public Type BuilderType { get; }
}
}
推荐阅读
- git - 创建一个适合功能更改的新分支是否仅限于 git 中的单个脚本文件?
- google-apps-script - 谷歌脚本如何比较两列中的单元格并将值设置为另一个单元格
- excel - 当工作簿受 vba 保护时,如何修复禁用的 excel 宏文件?
- java - 如何更改 NatTable 列 AutoSize 拖动图标
- gradle - 摇篮。说依赖来使用另一个版本的已经包含的依赖
- json - 如何用键的虚线名称形成json主体
- php - 我如何在 laravel 中计算月份明智的今天?
- sprite-kit - Sprite Action 在一段时间后停止工作
- javascript - 如何允许重复的反应语义 ui 下拉菜单显示不同的值
- python - 由于不支持地址系列错误,无法从 Python 发送电子邮件