Exemplo n.º 1
0
        private static void Compile(IDynamicActivity dynamicActivity, LocationReferenceEnvironment environment)
        {
#if NET45
            string language = null;
            if (RequiresCompilation(dynamicActivity, environment, out language))
            {
                TextExpressionCompiler        compiler = new TextExpressionCompiler(GetCompilerSettings(dynamicActivity, language));
                TextExpressionCompilerResults results  = compiler.Compile();

                if (results.HasErrors)
                {
                    StringBuilder messages = new StringBuilder();
                    messages.Append("\r\n");
                    messages.Append("\r\n");

                    foreach (TextExpressionCompilerError message in results.CompilerMessages)
                    {
                        messages.Append("\t");
                        if (results.HasSourceInfo)
                        {
                            messages.Append(string.Concat(" ", SR.ActivityXamlServiceLineString, " ", message.SourceLineNumber, ": "));
                        }
                        messages.Append(message.Message);
                    }

                    messages.Append("\r\n");
                    messages.Append("\r\n");

                    InvalidOperationException exception = new InvalidOperationException(SR.ActivityXamlServicesCompilationFailed(messages.ToString()));

                    foreach (TextExpressionCompilerError message in results.CompilerMessages)
                    {
                        exception.Data.Add(message, message.Message);
                    }
                    throw FxTrace.Exception.AsError(exception);
                }

                Type compiledExpressionRootType = results.ResultType;

                ICompiledExpressionRoot compiledExpressionRoot = Activator.CreateInstance(compiledExpressionRootType, new object[] { dynamicActivity }) as ICompiledExpressionRoot;
                CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpressionRoot);
            }
#endif
        }
        TextExpressionCompilerResults CompileInMemory()
        {
            List<TextExpressionCompilerError> messages = new List<TextExpressionCompilerError>();
            CompilerParameters compilerParameters = GetCompilerParameters(messages);
            
            CompilerResults compilerResults = null;
            using (CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider(this.settings.Language))
            {
                compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, this.compileUnit);
            }

            TextExpressionCompilerResults results = new TextExpressionCompilerResults();

            if (compilerResults.Errors == null || !compilerResults.Errors.HasErrors)
            {
                results.ResultType = compilerResults.CompiledAssembly.GetType(this.activityFullName);
            }

            results.HasSourceInfo = this.symbols != null;

            bool hasErrors = false;
            if (compilerResults.Errors != null && (compilerResults.Errors.HasWarnings || compilerResults.Errors.HasErrors))
            {

                foreach (CompilerError ce in compilerResults.Errors)
                {
                    TextExpressionCompilerError message = new TextExpressionCompilerError();

                    message.Message = ce.ErrorText;
                    message.Number = ce.ErrorNumber;

                    if (results.HasSourceInfo)
                    {
                        message.SourceLineNumber = ce.Line;
                    }
                    else
                    {
                        message.SourceLineNumber = -1;
                    }

                    message.IsWarning = ce.IsWarning;

                    messages.Add(message);

                    hasErrors |= !message.IsWarning;
                }
            }

            if (messages != null && messages.Count > 0)
            {
                results.SetMessages(messages, hasErrors);
            }

            return results;
        }