Exemplo n.º 1
0
        public CompilationResults Compile(string template)
        {
            Host.DefaultClassName = "Template";
            Host.DefaultNamespace = "__CompiledTemplates";
            RazorTemplateEngine engine = new RazorTemplateEngine(Host);
            GeneratorResults    generatorResults;

            using (TextReader reader = new StringReader(template))
            {
                generatorResults = engine.GenerateCode(reader);
            }

            if (!generatorResults.Success)
            {
                return(new CompilationResults(generatorResults, new List <CompilerError>()));
            }

            // Compile the code to a temporary assembly
            CodeDomProvider provider = Activator.CreateInstance(_language.CodeDomProviderType) as CodeDomProvider;

            if (provider == null)
            {
                throw new InvalidCastException(String.Format("Unable to convert '{0}' to a CodeDomProvider", _language.CodeDomProviderType.FullName));
            }
            var compilerResults = provider.CompileAssemblyFromDom(new CompilerParameters(new [] { typeof(TemplateBase).Assembly.Location }), generatorResults.GeneratedCode);

            if (compilerResults.Errors.HasErrors)
            {
                return(new CompilationResults(generatorResults, compilerResults.Errors.Cast <CompilerError>().ToList()));
            }
            var type = compilerResults.CompiledAssembly.GetType("__CompiledTemplates.Template");

            if (type == null)
            {
                throw new MissingMemberException("Unable to find compiled template in assembly");
            }
            TemplateBase compiled = Activator.CreateInstance(type) as TemplateBase;

            if (compiled == null)
            {
                throw new InvalidCastException("Unable to convert template to TemplateBase");
            }
            return(new CompilationResults(generatorResults, compiled));
        }
Exemplo n.º 2
0
 public CompilationResults(GeneratorResults generatorResults, TemplateBase compiled)
     : base(true, generatorResults.Document, generatorResults.ParserErrors, generatorResults.GeneratedCode, generatorResults.DesignTimeLineMappings)
 {
     Compiled = compiled;
 }
Exemplo n.º 3
0