示例#1
0
 private static void CacheAssembly(Assembly assembly)
 {
     if (assembly == null)
     {
         return;
     }
     if (AssemblyCache.Any((ka) => ka.FullName == assembly.FullName))
     {
         return;
     }
     AssemblyCache.Add(assembly);
 }
        public void AddFile(string file)
        {
            Assembly assembly = Assembly.LoadFrom(file);

            _cache.Add(assembly.GetName().FullName, assembly);
        }
示例#3
0
        /// <summary>
        /// Parses and compiles a markup template into an assembly and returns
        /// an assembly name. The name is an ID that can be passed to
        /// ExecuteTemplateByAssembly which picks up a cached instance of the
        /// loaded assembly.
        ///
        /// </summary>
        /// <param name="ReferencedAssemblies">Any referenced assemblies by dll name only. Assemblies must be in execution path of host or in GAC.</param>
        /// <param name="templateSourceReader">Textreader that loads the template</param>
        /// <param name="generatedNamespace">The namespace of the class to generate from the template. null generates name.</param>
        /// <param name="generatedClassName">The name of the class to generate from the template. null generates name.</param>
        /// <remarks>
        /// The actual assembly isn't returned here to allow for cross-AppDomain
        /// operation. If the assembly was returned it would fail for cross-AppDomain
        /// calls.
        /// </remarks>
        /// <returns>An assembly Id. The Assembly is cached in memory and can be used with RenderFromAssembly.</returns>
        public string ParseAndCompileTemplate(
            string[] ReferencedAssemblies,
            TextReader templateSourceReader,
            string generatedNamespace,
            string generatedClassName)
        {
            if (string.IsNullOrEmpty(generatedNamespace))
            {
                generatedNamespace = "__RazorHost";
            }
            if (string.IsNullOrEmpty(generatedClassName))
            {
                generatedClassName = GetSafeClassName(null);
            }

            RazorTemplateEngine engine = CreateHost(generatedNamespace, generatedClassName);

            // Generate the template class as CodeDom
            GeneratorResults razorResults = engine.GenerateCode(templateSourceReader);

            // Create code from the codeDom and compile
            CSharpCodeProvider   codeProvider = new CSharpCodeProvider();
            CodeGeneratorOptions options      = new CodeGeneratorOptions();

            // Capture Code Generated as a string for error info
            // and debugging
            LastGeneratedCode = null;
            using (StringWriter writer = new StringWriter())
            {
                codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, writer, options);
                LastGeneratedCode = writer.ToString();
            }

            CompilerParameters compilerParameters = new CompilerParameters(ReferencedAssemblies);

            // Standard Assembly References
            compilerParameters.ReferencedAssemblies.Add("System.dll");
            compilerParameters.ReferencedAssemblies.Add("System.Core.dll");
            compilerParameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");               // dynamic support!

            // Also add the current assembly so RazorTemplateBase is available
            compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().CodeBase.Substring(8));

            compilerParameters.GenerateInMemory = Configuration.CompileToMemory;
            if (!Configuration.CompileToMemory)
            {
                compilerParameters.OutputAssembly = Path.Combine(Configuration.TempAssemblyPath, "_" + Guid.NewGuid().ToString("n") + ".dll");
            }

            CompilerResults compilerResults = codeProvider.CompileAssemblyFromDom(compilerParameters, razorResults.GeneratedCode);

            if (compilerResults.Errors.Count > 0)
            {
                var compileErrors = new StringBuilder();
                foreach (CompilerError compileError in compilerResults.Errors)
                {
                    compileErrors.Append(String.Format(Resources.LineX0TColX1TErrorX2RN, compileError.Line, compileError.Column, compileError.ErrorText));
                }

                this.SetError(compileErrors + "\r\n" + LastGeneratedCode);
                return(null);
            }

            AssemblyCache.Add(compilerResults.CompiledAssembly.FullName, compilerResults.CompiledAssembly);

            return(compilerResults.CompiledAssembly.FullName);
        }