目录
using Flatwhite.Core.Provider;
namespace Flatwhite.Core
{
/// <summary>
/// Global config
/// </summary>
public static class Global
{
/// <summary>
/// Internal cache for Flatwhite objects
/// </summary>
internal static MethodInfoCache Cache { get; set; }
static Global()
{
Init();
}
internal static void Init()
{
Cache = new MethodInfoCache();
}
}
}
using System;
using System.Threading.Tasks;
namespace Flatwhite.Core
{
internal class InvocationAttribute : MethodFilterAttribute
{
private readonly IInvocation _invocation;
private readonly Type _taskGenericReturnType;
public InvocationAttribute(IInvocation invocation, Type taskGenericReturnType = null)
{
_invocation = invocation;
_taskGenericReturnType = taskGenericReturnType;
}
public override void OnMethodExecuting(MethodExecutingContext methodExecutingContext)
{
_invocation.Proceed();
methodExecutingContext.Result = _invocation.ReturnValue;
}
public override async Task OnMethodExecutingAsync(MethodExecutingContext actionContext)
{
_invocation.Proceed();
if (_invocation.ReturnValue is Task taskResult)
{
if (_taskGenericReturnType != null)
{
actionContext.Result = await taskResult.TryGetTaskResult();
}
else
{
await taskResult;
}
}
else
actionContext.Result = _invocation.ReturnValue;
}
}
}