internal static IEmitResult Emit(this ICompilation compilation)
 {
     using (var stream = new MemoryStream())
     {
         return(compilation.Emit(stream));
     }
 }
        private MethodInfo CreateFunctionTarget(CancellationToken cancellationToken)
        {
            MemoryStream assemblyStream = null;
            MemoryStream pdbStream      = null;

            try
            {
                ICompilation      compilation       = _compilationService.GetFunctionCompilation(Metadata);
                FunctionSignature functionSignature = compilation.GetEntryPointSignature(_functionEntryPointResolver);

                ImmutableArray <Diagnostic> bindingDiagnostics = ValidateFunctionBindingArguments(functionSignature, throwIfFailed: true);
                TraceCompilationDiagnostics(bindingDiagnostics);

                using (assemblyStream = new MemoryStream())
                {
                    using (pdbStream = new MemoryStream())
                    {
                        compilation.Emit(assemblyStream, pdbStream, cancellationToken);

                        // Check if cancellation was requested while we were compiling,
                        // and if so quit here.
                        cancellationToken.ThrowIfCancellationRequested();

                        Assembly assembly = Assembly.Load(assemblyStream.GetBuffer(), pdbStream.GetBuffer());
                        _assemblyLoader.CreateOrUpdateContext(Metadata, assembly, _metadataResolver, TraceWriter);

                        // Get our function entry point
                        _functionSignature = functionSignature;
                        System.Reflection.TypeInfo scriptType = assembly.DefinedTypes
                                                                .FirstOrDefault(t => string.Compare(t.Name, functionSignature.ParentTypeName, StringComparison.Ordinal) == 0);

                        return(_functionEntryPointResolver.GetFunctionEntryPoint(scriptType.DeclaredMethods.ToList()));
                    }
                }
            }
            catch (CompilationErrorException ex)
            {
                TraceWriter.Error("Function compilation error");
                TraceCompilationDiagnostics(ex.Diagnostics);
                throw;
            }
        }