Example #1
0
        public void Intercept(IInvocation invocation)
        {
            if (!invocation.HasValidReturnType())
            {
                invocation.ReturnValue = null;
                return;
            }

            var tcsType = typeof(TaskCompletionSource <>).MakeGenericType(invocation.Method.ReturnType.GetGenericArguments()[0]);
            var tcs     = Activator.CreateInstance(tcsType);

            invocation.ReturnValue = tcsType.GetProperty("Task").GetValue(tcs, null);

#pragma warning disable CA2008 // Do not create tasks without passing a TaskScheduler
            InterceptAsync(invocation).ContinueWith(_ =>
            {
                tcsType.GetMethod("SetResult").Invoke(tcs, new[] { invocation.ReturnValue });
            });
#pragma warning restore CA2008 // Do not create tasks without passing a TaskScheduler
        }