Reputation: 9360
Hello guys i am trying to create my own Task
-Like types using the AsyncMethodBuilderAttribute
for the .NetCore framework.
So far in .Net Core
my custom Type
works and it can be awaited
and i can return it as async [MyTypeName]<T> MyMethod()
instead of Task
.
However in .NetStandard 2.0
the attribute is not present and and i tried to implement it:
I decorated my awaitable type with the attribute but it still does not let me use it: "The return type of an async method must be void,Task,Task<T>"
So far i can use something like this in .Net Core
and it works !!
public async Task WrapperMethod(){
int result=await GetIntAsync();
}
public async Errand<int> GetIntAsync()
{
await Task.Delay(1000);
return 3;
}
Attribute:
[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; }
}
Below are the implementation of the custom type and the async method builder
.The awaiter
is not important in this case but i can provide the source if needed.
My Task
-Like Type
[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;
}
}
The custom AsyncMethodBuilder
implementation:
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);
}
What else should i add for it to work in .Net Standard?
Upvotes: 5
Views: 1814
Reputation: 1499760
It doesn't matter where the source file is for the attribute, but it does need to have the right namespace declaration:
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; }
}
}
Upvotes: 4