Below is the code from the Intercept
method on a custom type that implements IInterceptor
of the Castle Dynamic Proxy library. This snippet is from
Trying to clarify with a generic and clean solution for:
async
methods adding custom code as a continuation task. I think the best solution is to use the dynamic
keyword to bypass the compiler type checking and resolve the difference between Task and Task
at run time:
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
var method = invocation.MethodInvocationTarget;
var isAsync = method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) != null;
if (isAsync && typeof(Task).IsAssignableFrom(method.ReturnType))
{
invocation.ReturnValue = InterceptAsync((dynamic)invocation.ReturnValue);
}
}
private static async Task InterceptAsync(Task task)
{
await task.ConfigureAwait(false);
// do the logging here, as continuation work for Task...
}
private static async Task InterceptAsync(Task task)
{
T result = await task.ConfigureAwait(false);
// do the logging here, as continuation work for Task...
return result;
}