public static object AssemblyRunner_RunAsync(
            object xunitTestAssemblyRunner,
            object messageBus,
            object testCollection,
            object testCases,
            object cancellationTokenSource,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            if (xunitTestAssemblyRunner == null)
            {
                throw new ArgumentNullException(nameof(xunitTestAssemblyRunner));
            }

            Type xunitTestAssemblyRunnerType = xunitTestAssemblyRunner.GetType();
            Func <object, object, object, object, object, object> executeAsync;

            try
            {
                executeAsync =
                    MethodBuilder <Func <object, object, object, object, object, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, XUnitRunTestCollectionAsyncMethod)
                    .WithConcreteType(xunitTestAssemblyRunnerType)
                    .WithParameters(messageBus, testCollection, testCases, cancellationTokenSource)
                    .WithNamespaceAndNameFilters("System.Threading.Tasks.Task`1<Xunit.Sdk.RunSummary>", "Xunit.Sdk.IMessageBus", "Xunit.Abstractions.ITestCollection", "System.Collections.Generic.IEnumerable`1<T>", "System.Threading.CancellationTokenSource")
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: XUnitTestAssemblyRunnerType,
                    methodName: XUnitRunTestCollectionAsyncMethod,
                    instanceType: xunitTestAssemblyRunnerType.AssemblyQualifiedName);
                throw;
            }

            object    returnValue = null;
            Exception exception   = null;

            try
            {
                returnValue = executeAsync(xunitTestAssemblyRunner, messageBus, testCollection, testCases, cancellationTokenSource);
            }
            catch (TargetInvocationException ex)
            {
                exception = ex.InnerException;
                throw;
            }
            catch (Exception ex)
            {
                exception = ex;
                throw;
            }
            finally
            {
                returnValue = AsyncTool.AddContinuation <object>(
                    returnValue,
                    exception,
                    null,
                    async(r, ex, state) =>
                {
                    // We have to ensure the flush of the buffer after we finish the tests of an assembly.
                    // For some reason, sometimes when all test are finished none of the callbacks to handling the tracer disposal is triggered.
                    // So the last spans in buffer aren't send to the agent.
                    // Other times we reach the 500 items of the buffer in a sec and the tracer start to drop spans.
                    // In a test scenario we must keep all spans.
                    await Common.TestTracer.FlushAsync().ConfigureAwait(false);
                    return(r);
                });
            }

            return(returnValue);
        }
        public static object TestInvoker_RunAsync(
            object testInvoker,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            if (testInvoker == null)
            {
                throw new ArgumentNullException(nameof(testInvoker));
            }

            Type testInvokerType = testInvoker.GetType();
            Func <object, object> executeAsync;

            try
            {
                executeAsync =
                    MethodBuilder <Func <object, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, XUnitRunAsyncMethod)
                    .WithConcreteType(testInvokerType)
                    .WithDeclaringTypeGenerics(testInvokerType.BaseType.GenericTypeArguments)
                    .WithNamespaceAndNameFilters("System.Threading.Tasks.Task`1<System.Decimal>")
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: XUnitTestInvokerType,
                    methodName: XUnitRunAsyncMethod,
                    instanceType: testInvokerType.AssemblyQualifiedName);
                throw;
            }

            object    returnValue = null;
            Exception exception   = null;

            try
            {
                returnValue = executeAsync(testInvoker);
            }
            catch (TargetInvocationException ex)
            {
                exception = ex.InnerException;
                throw;
            }
            catch (Exception ex)
            {
                exception = ex;
                throw;
            }
            finally
            {
                returnValue = AsyncTool.AddContinuation(returnValue, exception, testInvoker, (r, ex, state) => InvokerContinuation(r, ex, state));
            }

            return(returnValue);
        }
        public static object TestRunner_RunAsync(
            object testRunner,
            int opCode,
            int mdToken,
            long moduleVersionPtr)
        {
            if (testRunner == null)
            {
                throw new ArgumentNullException(nameof(testRunner));
            }

            Type testRunnerType = testRunner.GetType();
            Func <object, object> executeAsync;

            try
            {
                executeAsync =
                    MethodBuilder <Func <object, object> >
                    .Start(moduleVersionPtr, mdToken, opCode, XUnitRunAsyncMethod)
                    .WithConcreteType(testRunnerType)
                    .WithDeclaringTypeGenerics(testRunnerType.BaseType.GenericTypeArguments)
                    .WithNamespaceAndNameFilters(ClrNames.GenericTask)
                    .Build();
            }
            catch (Exception ex)
            {
                Log.ErrorRetrievingMethod(
                    exception: ex,
                    moduleVersionPointer: moduleVersionPtr,
                    mdToken: mdToken,
                    opCode: opCode,
                    instrumentedType: XUnitTestRunnerType,
                    methodName: XUnitRunAsyncMethod,
                    instanceType: testRunnerType.AssemblyQualifiedName);
                throw;
            }

            Scope scope = CreateScope(testRunner);

            if (scope is null)
            {
                return(executeAsync(testRunner));
            }

            object    returnValue = null;
            Exception exception   = null;

            try
            {
                // reset the start time of the span just before running the test
                scope.Span.ResetStartTime();

                // starts the test execution
                returnValue = executeAsync(testRunner);
            }
            catch (TargetInvocationException ex)
            {
                exception = ex.InnerException;
                throw;
            }
            catch (Exception ex)
            {
                exception = ex;
                throw;
            }
            finally
            {
                returnValue = AsyncTool.AddContinuation(returnValue, exception, scope, (r, ex, state) => TestRunnerContinuation(r, ex, state));
            }

            return(returnValue);
        }