예제 #1
0
        public Type Compile <TTemplate>(
            string templateContents,
            string className,
            ITemplateCodeBuilder templateCodeBuilder,
            CodeDomProvider codeDomProvider,
            Action <CodeTypeDeclaration> typeConfigurationDelegate,
            IEnumerable <string> namespaceImports)
            where TTemplate : ITemplate
        {
            templateContents.ThrowIfNull("templateContents");
            className.ThrowIfNull("className");
            templateCodeBuilder.ThrowIfNull("templateCodeBuilder");
            codeDomProvider.ThrowIfNull("codeDomProvider");
            namespaceImports.ThrowIfNull("namespaceImports");

            BuildCodeResult buildCodeResult = templateCodeBuilder.BuildCode <TTemplate>(templateContents, className, typeConfigurationDelegate, namespaceImports);
            var             parameters      = new CompilerParameters
            {
                GenerateInMemory        = true,
                GenerateExecutable      = false,
                IncludeDebugInformation = false,
                CompilerOptions         = "/target:library /optimize"
            };

            string[] assemblyLocations = _referenceResolver.ResolveAssemblyLocations().ToArray();

            parameters.ReferencedAssemblies.AddRange(assemblyLocations);

            CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(parameters, buildCodeResult.CompileUnit);

            if (compilerResults.Errors != null && compilerResults.Errors.Count > 0)
            {
                throw new TemplateCompilationException(compilerResults.Errors);
            }

            return(compilerResults.CompiledAssembly.GetType(buildCodeResult.TypeFullName));
        }