Class Conversion
Provides task result conversion methods.
Inherited Members
Namespace: DotNext.Threading.Tasks
Assembly: DotNext.dll
Syntax
public static class Conversion
Methods
| Edit this page View SourceAsDynamic(Task)
Allows to convert Task<TResult> of unknown result type into dynamically
typed task which result can be obtained as object
or any other data type using dynamic approach.
Declaration
public static DynamicTaskAwaitable AsDynamic(this Task task)
Parameters
| Type | Name | Description |
|---|---|---|
| Task | task | The arbitrary task of type Task<TResult>. |
Returns
| Type | Description |
|---|---|
| DynamicTaskAwaitable | The dynamically typed task. |
Remarks
The type of the returned task is not known at compile time and therefore treated as dynamic. The result value returned
by await operator is equal to Value if task is not of type Task<TResult>.
CatchException(Task)
Suspends the exception that can be raised by the task, and return it as an object if raised.
Declaration
public static ExceptionCapturingAwaitable CatchException(this Task task)
Parameters
| Type | Name | Description |
|---|---|---|
| Task | task | The task. |
Returns
| Type | Description |
|---|---|
| ExceptionCapturingAwaitable | The awaitable object that returns the exception if it was raised by the underlying task. |
CatchException(ValueTask)
Suspends the exception that can be raised by the task, and return it as an object if raised.
Declaration
public static ExceptionCapturingAwaitable CatchException(this ValueTask task)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask | task | The task. |
Returns
| Type | Description |
|---|---|
| ExceptionCapturingAwaitable | The awaitable object that returns the exception if it was raised by the underlying task. |
CatchException<T>(Task<T>)
Returns a task that never throws an exception.
Declaration
public static AwaitableResult<T> CatchException<T>(this Task<T> task)
Parameters
| Type | Name | Description |
|---|---|---|
| Task<T> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
CatchException<T>(ValueTask<T>)
Returns a task that never throws an exception.
Declaration
public static AwaitableResult<T> CatchException<T>(this ValueTask<T> task)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask<T> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
CatchException<T, TError>(Task<T>, Converter<Exception, TError>)
Returns a task that never throws an exception.
Declaration
public static AwaitableResult<T, TError> CatchException<T, TError>(this Task<T> task, Converter<Exception, TError> converter) where TError : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| Task<T> | task | The task to convert. |
| Converter<Exception, TError> | converter | The exception converter. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T, TError> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
| TError | The type of the error. |
CatchException<T, TError>(ValueTask<T>, Converter<Exception, TError>)
Returns a task that never throws an exception.
Declaration
public static AwaitableResult<T, TError> CatchException<T, TError>(this ValueTask<T> task, Converter<Exception, TError> converter) where TError : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask<T> | task | The task to convert. |
| Converter<Exception, TError> | converter | The exception converter. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T, TError> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
| TError | The type of the error. |
Convert<TInput, TOutput>(Task<TInput>)
Converts one type of task into another.
Declaration
public static Task<TOutput> Convert<TInput, TOutput>(this Task<TInput> task) where TInput : TOutput
Parameters
| Type | Name | Description |
|---|---|---|
| Task<TInput> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| Task<TOutput> | The converted task. |
Type Parameters
| Name | Description |
|---|---|
| TInput | The source task type. |
| TOutput | The target task type. |
Convert<TInput, TOutput>(Task<TInput>, Converter<TInput, Task<TOutput>>)
Converts one type of task into another.
Declaration
public static Task<TOutput> Convert<TInput, TOutput>(this Task<TInput> task, Converter<TInput, Task<TOutput>> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| Task<TInput> | task | The task to convert. |
| Converter<TInput, Task<TOutput>> | converter | Asynchronous conversion function. |
Returns
| Type | Description |
|---|---|
| Task<TOutput> | The converted task. |
Type Parameters
| Name | Description |
|---|---|
| TInput | The source task type. |
| TOutput | The target task type. |
Convert<TInput, TOutput>(Task<TInput>, Converter<TInput, TOutput>)
Converts one type of task into another.
Declaration
public static Task<TOutput> Convert<TInput, TOutput>(this Task<TInput> task, Converter<TInput, TOutput> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| Task<TInput> | task | The task to convert. |
| Converter<TInput, TOutput> | converter | Non-blocking conversion function. |
Returns
| Type | Description |
|---|---|
| Task<TOutput> | The converted task. |
Type Parameters
| Name | Description |
|---|---|
| TInput | The source task type. |
| TOutput | The target task type. |
SuspendException(Task, Predicate<Exception>)
Suspends the exception that can be raised by the task.
Declaration
public static SuspendedExceptionTaskAwaitable SuspendException(this Task task, Predicate<Exception> filter)
Parameters
| Type | Name | Description |
|---|---|---|
| Task | task | The task. |
| Predicate<Exception> | filter | The filter of the exception to be suspended. |
Returns
| Type | Description |
|---|---|
| SuspendedExceptionTaskAwaitable | The awaitable object that suspends exceptions according to the filter. |
SuspendException(ValueTask, Predicate<Exception>)
Suspends the exception that can be raised by the task.
Declaration
public static SuspendedExceptionTaskAwaitable SuspendException(this ValueTask task, Predicate<Exception> filter)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask | task | The task. |
| Predicate<Exception> | filter | The filter of the exception to be suspended. |
Returns
| Type | Description |
|---|---|
| SuspendedExceptionTaskAwaitable | The awaitable object that suspends exceptions according to the filter. |
SuspendException<TArg>(Task, TArg, Func<Exception, TArg, bool>)
Suspends the exception that can be raised by the task.
Declaration
public static SuspendedExceptionTaskAwaitable<TArg> SuspendException<TArg>(this Task task, TArg arg, Func<Exception, TArg, bool> filter)
Parameters
| Type | Name | Description |
|---|---|---|
| Task | task | The task. |
| TArg | arg | The argument to be passed to the filter. |
| Func<Exception, TArg, bool> | filter | The filter of the exception to be suspended. |
Returns
| Type | Description |
|---|---|
| SuspendedExceptionTaskAwaitable<TArg> | The awaitable object that suspends exceptions according to the filter. |
Type Parameters
| Name | Description |
|---|---|
| TArg |
SuspendException<T>(Task<T>)
Returns a task that never throws an exception.
Declaration
[Obsolete("Use CatchException() instead.")]
public static AwaitableResult<T> SuspendException<T>(this Task<T> task)
Parameters
| Type | Name | Description |
|---|---|---|
| Task<T> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
SuspendException<TArg>(ValueTask, TArg, Func<Exception, TArg, bool>)
Suspends the exception that can be raised by the task.
Declaration
public static SuspendedExceptionTaskAwaitable<TArg> SuspendException<TArg>(this ValueTask task, TArg arg, Func<Exception, TArg, bool> filter)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask | task | The task. |
| TArg | arg | The argument to be passed to the filter. |
| Func<Exception, TArg, bool> | filter | The filter of the exception to be suspended. |
Returns
| Type | Description |
|---|---|
| SuspendedExceptionTaskAwaitable<TArg> | The awaitable object that suspends exceptions according to the filter. |
Type Parameters
| Name | Description |
|---|---|
| TArg |
SuspendException<T>(ValueTask<T>)
Returns a task that never throws an exception.
Declaration
[Obsolete("Use CatchException() instead.")]
public static AwaitableResult<T> SuspendException<T>(this ValueTask<T> task)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask<T> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
SuspendException<T, TError>(Task<T>, Converter<Exception, TError>)
Returns a task that never throws an exception.
Declaration
[Obsolete("Use CatchException() instead.")]
public static AwaitableResult<T, TError> SuspendException<T, TError>(this Task<T> task, Converter<Exception, TError> converter) where TError : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| Task<T> | task | The task to convert. |
| Converter<Exception, TError> | converter | The exception converter. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T, TError> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
| TError | The type of the error. |
SuspendException<T, TError>(ValueTask<T>, Converter<Exception, TError>)
Returns a task that never throws an exception.
Declaration
[Obsolete("Use CatchException() instead.")]
public static AwaitableResult<T, TError> SuspendException<T, TError>(this ValueTask<T> task, Converter<Exception, TError> converter) where TError : struct, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| ValueTask<T> | task | The task to convert. |
| Converter<Exception, TError> | converter | The exception converter. |
Returns
| Type | Description |
|---|---|
| AwaitableResult<T, TError> | The task that never throws an exception. Instead, the Result<T> contains an exception. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the task. |
| TError | The type of the error. |
ToNullable<T>(Task<T>)
Converts value type into nullable value type.
Declaration
public static Task<T?> ToNullable<T>(this Task<T> task) where T : struct
Parameters
| Type | Name | Description |
|---|---|---|
| Task<T> | task | The task to convert. |
Returns
| Type | Description |
|---|---|
| Task<T?> | The converted task. |
Type Parameters
| Name | Description |
|---|---|
| T | The value type. |