/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TU">Result type of the function.</typeparam> /// <param name="function"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TU> GetTrace <TU>(Func <TU> function) { var result = new RunTraceResult <TU>(); CaptureTrace(() => { result.Result = function(); #if !DOTNET_20 && !DOTNET_30 && !DOTNET_35 && !DOTNET_40 && !PORTABLE var ta = result.Result as Task; if (ta == null) { return; } // we must check if the method is flagged async if (function.GetMethodInfo().GetCustomAttributes(false).Any(x => x.GetType().Name.StartsWith("AsyncStateMachineAttribute"))) { try { ta.Wait(); } catch (AggregateException exception) { result.RaisedException = exception.InnerException; } } #endif }, result); return(result); }
/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TU">Result type of the function.</typeparam> /// <param name="function"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TU> GetTrace <TU>(Func <TU> function) { var result = new RunTraceResult <TU>(); CaptureTrace(() => { result.Result = function(); #if !DOTNET_20 && !DOTNET_30 && !DOTNET_35 && !DOTNET_40 && !PORTABLE var ta = result.Result as Task; if (ta == null) { return; } // we must check if the method is flagged async if (!FunctionIsAsync(function)) { return; } try { ta.Wait(); } catch (AggregateException exception) { result.RaisedException = exception.InnerException; } #endif }, result); return(result); }
/// <summary> /// Execute the function to capture the run. /// </summary> /// <typeparam name="TResult">Result type of the awaitable function.</typeparam> /// <param name="waitableFunction"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTraceResult <TResult> GetAsyncTrace <TResult>(Func <Task <TResult> > waitableFunction) { var result = new RunTraceResult <TResult>(); CaptureTrace( () => { try { // starts and waits the completion of the awaitable method result.Result = waitableFunction().Result; } catch (AggregateException agex) { result.RaisedException = agex.InnerException; } }, result); return(result); }