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