コード例 #1
0
        /// <summary>
        ///     Post work for to be executed on another thread without regard to when it returns
        /// </summary>
        /// <param name="token"></param>
        /// <param name="callback"></param>
        /// <returns>An operation result that can be waited on</returns>
        public TryOperationResult <object> PostWork(CancellationToken token, DispatchedWork callback)
        {
            var result = new TryOperationResult <object>();

            _workers.Enqueue((source, token2) => {
                try {
                    result.SetSuccess(ExecuteCallback(token, callback, source, token2).Result);
                }
                catch (Exception ex) {
                    result.SetFailure(ex);
                }
                return(result);
            });
            return(result);
        }
コード例 #2
0
        public async Task <TryOperationResult <TResult> > RunOperationAsync <TResult>(
            Func <IServiceProvider, TResult> operation)
        {
            InitializeServiceProvider();
            return(await new Task <TryOperationResult <TResult> >(() => {
                var result = new TryOperationResult <TResult>();

                try {
                    result.SetSuccess(operation(Provider));
                }
                catch (Exception ex) {
                    result.SetFailure(ex);
                }

                return result;
            }));
        }
コード例 #3
0
        public List <Diagnostic> Compile(BuildProject project, out TryOperationResult <Assembly> assembly)
        {
            var ret = new List <Diagnostic>();

            assembly = new TryOperationResult <Assembly>();
            try {
                var assemblyName = project.ProjectName ?? Path.GetRandomFileName();
                var references   = new Dictionary <string, MetadataReference>();
                foreach (var reference in project.References)
                {
                    var asmRef = MetadataReference.CreateFromFile(reference);
                    references[asmRef.FilePath] = asmRef;
                }


                var compilation = CSharpCompilation.Create(
                    assemblyName,
                    project.Files.Select(code => CSharpSyntaxTree.ParseText(code.Content)).ToArray(),
                    references.Values,
                    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                using (var ms = new MemoryStream()) {
                    var result = compilation.Emit(ms);

                    ret.AddRange(result.Diagnostics);
                    if (result.Success)
                    {
                        ms.Position = 0;
                        assembly.SetSuccess(Assembly.Load(ms.ToArray()));
                    }
                }
            }
            catch (Exception ex) {
                assembly.SetFailure(ex);
            }

            return(ret);
        }