<PackageReference Include="NUnit" Version="4.1.0" />

ValueTaskAwaitAdapter

static class ValueTaskAwaitAdapter
using System; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace NUnit.Framework.Internal { internal static class ValueTaskAwaitAdapter { private sealed class NonGenericAdapter : AwaitAdapter { private readonly ValueTaskAwaiter _awaiter; public override bool IsCompleted => _awaiter.IsCompleted; public NonGenericAdapter(ValueTask task) { _awaiter = task.GetAwaiter(); } [System.Runtime.CompilerServices.NullableContext(1)] public override void OnCompleted(Action action) { _awaiter.UnsafeOnCompleted(action); } public override void BlockUntilCompleted() { _awaiter.GetResult(); } [System.Runtime.CompilerServices.NullableContext(2)] public override object GetResult() { _awaiter.GetResult(); return null; } } [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] private sealed class GenericAdapter<T> : AwaitAdapter { [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] private readonly ValueTaskAwaiter<T> _awaiter; public override bool IsCompleted => _awaiter.IsCompleted; public GenericAdapter([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ValueTask<T> task) { _awaiter = task.GetAwaiter(); } [System.Runtime.CompilerServices.NullableContext(1)] public override void OnCompleted(Action action) { _awaiter.UnsafeOnCompleted(action); } public override void BlockUntilCompleted() { _awaiter.GetResult(); } public override object GetResult() { return _awaiter.GetResult(); } } [System.Runtime.CompilerServices.NullableContext(1)] public static AwaitAdapter Create(ValueTask task) { Type type = task.GetType().TypeAndBaseTypes().FirstOrDefault(delegate(Type t) { if (t.IsGenericType) return t.GetGenericTypeDefinition() == typeof(ValueTask<>); return false; }); if ((object)type != null) { Type type2 = type.GetGenericArguments()[0]; return (AwaitAdapter)typeof(GenericAdapter<>).MakeGenericType(type2).GetConstructor(new Type[1] { typeof(ValueTask<>).MakeGenericType(type2) }).Invoke(new object[1] { task }); } return new NonGenericAdapter(task); } } }