Exemplo n.º 1
0
        public void Invoke(object[] parameters)
        {
            // Create compiled type:
            if (this.codeBuilder == null)
            {
                this.host.Log("Parsing template: \"{0}\".", this.templatefileinfo.FullName);
                this.fileContent = this.ReadAndParseFile();
                this.ReadDirectives(this.fileContent);

                this.host.Log("Compiling template: \"{0}\".", this.templatefileinfo.FullName);
                this.codeBuilder = this.CreateCodeBuilder(this.GetCodeTemplateDirective()["Language"] ?? GenerationLanguage.DefaultTemplateLanguage);
                this.codeBuilder.TemplateInfo = this;
                bool succeeded = this.codeBuilder.Compile();
                foreach (CompilerError err in this.codeBuilder.CompilerErrors)
                {
                    this.host.Log("Compile {0} {1}: {2}\r\n  File \"{3}\", line {4}",
                                  err.IsWarning ? "warning" : "error",
                                  err.ErrorNumber,
                                  err.ErrorText,
                                  err.FileName,
                                  err.Line);
                }
                if (succeeded == false)
                {
                    throw new CompilationFailedException(this.templatefileinfo.FullName, this.codeBuilder.CompilerErrors);
                }
            }

            // Construct full parameters (add host):
            List <object> allparameters = new List <object>();

            allparameters.Add(this.host);
            allparameters.AddRange(parameters);

            // Create instance and invoke:
            this.host.Log("Invoking template: \"{0}\".", this.templatefileinfo.FullName);
            CodeTemplate instance = (CodeTemplate)Activator.CreateInstance(this.codeBuilder.CompiledType, allparameters.ToArray());

            try
            {
                instance.Generate();
            }
            catch (RuntimeException)
            {
                throw;
            }
            catch (CompilationFailedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                this.host.Log(ex.ToString());
                throw new RuntimeException(ex);
            }
        }