mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-15 06:43:37 +01:00
* Add new package WireMock.Net.Extensions.Routing * Update documentation for WireMock.Net.Extensions.Routing * Cleanup imports * Add header to all source files inside WireMock.Net.Extensions.Routing * Add header to all source files inside WireMock.Net.Extensions.Routing.Tests * Revert unintended changes * Remove redundant build configurations * Remove incorrect links from documentation * Update nuget package references * Revert unintended changes * Migrate to AwesomeAssertions * Remove redundant project reference * Adjust formatting * Migrate to primary constructor * Refactoring: rename delegate parameter * Abstract over JSON converter * Replace WireMock with WireMock.Net in comments * Move local functions to the bottom of the methods
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Reflection;
|
|
|
|
namespace WireMock.Net.Extensions.Routing.Extensions;
|
|
|
|
internal static class TaskExtensions
|
|
{
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage(
|
|
"Usage",
|
|
"VSTHRD003:Avoid awaiting foreign Tasks",
|
|
Justification = "Await is required here to transform base task to generic one.")]
|
|
public static async Task<object?> ToGenericTaskAsync(this Task task)
|
|
{
|
|
await task;
|
|
var taskType = task.GetType();
|
|
if (!IsAssignableToGenericTaskType(taskType))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return task
|
|
.GetType()
|
|
.GetProperty("Result", BindingFlags.Instance | BindingFlags.Public)!
|
|
.GetValue(task);
|
|
}
|
|
|
|
private static bool IsAssignableToGenericTaskType(Type type)
|
|
{
|
|
if (type.IsGenericType &&
|
|
type.GetGenericTypeDefinition() == typeof(Task<>) &&
|
|
type.GetGenericArguments()[0] != Type.GetType("System.Threading.Tasks.VoidTaskResult"))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return type.BaseType is not null && IsAssignableToGenericTaskType(type.BaseType);
|
|
}
|
|
}
|