Open sandboxFocus

Class CurrentTaskAttribute

Binds a parameter of a method boundary advice to the task that the caller of the current async method observes. The parameter is null until the method has yielded, and always null when the method is not an async method, when it is implemented with runtime async, or when its method builder is not supported.

Inheritance
CurrentTaskAttribute
Namespace: PostSharp.Aspects.Advices
Assembly: PostSharp.dll
Syntax
public sealed class CurrentTaskAttribute : AdviceParameterAttribute
Remarks

The task exists once the method has yielded, that is, once one of its await expressions has suspended it. This applies to every async method, whether it returns Task, Task<TResult>, ValueTask or ValueTask<TResult>. The parameter is the task in the resume advice, in a yield advice other than the first one, and in the success, exception and exit advices of an execution that yielded. It is null in the entry advice, in the first yield advice, and in the advices of an execution that completes without yielding. Reading the task of the method builder earlier would create a task that the first await replaces on .NET.

The supported method builders are AsyncTaskMethodBuilder, AsyncTaskMethodBuilder<TResult>, AsyncValueTaskMethodBuilder and AsyncValueTaskMethodBuilder<TResult>. For a method that returns ValueTask or ValueTask<TResult>, the task is the one that the ValueTask of the caller wraps. Other builders, such as PoolingAsyncValueTaskMethodBuilder, give null. A method implemented with runtime async has no task while it runs, and the compiler reports a warning.

The parameter must be of type Task, IAsyncResult or object, and it cannot be ref, in or out. This attribute can be used only in the method boundary advices.

Constructors

Name Description
CurrentTaskAttribute()

Properties

Name Description
IsRequired

Gets or sets a value indicating whether the parameter must never be null. When this property is true, the compiler reports an error when the advice can run before the method yields, which is the case of every advice except the resume advice, and when the target method cannot provide its task. The resume advice can also run before the method yields when the entry advice of its group yields on an awaiter that is already completed, so the compiler also reports an error when that entry advice has an AwaiterAttribute parameter. The default value is false.

See Also