private Tuple<CompilerResults, string> Compile(TypeContext context) { if (_disposed) throw new ObjectDisposedException(GetType().Name); var compileUnit = GetCodeCompileUnit(context.ClassName, context.TemplateContent, context.Namespaces, context.TemplateType, context.ModelType); var @params = new CompilerParameters { GenerateInMemory = true, GenerateExecutable = false, IncludeDebugInformation = false, CompilerOptions = "/target:library /optimize" }; var assemblies = CompilerServicesUtility .GetLoadedAssemblies() .Where(a => !a.IsDynamic && File.Exists(a.Location)) .GroupBy(a => a.GetName().Name).Select(grp => grp.First(y => y.GetName().Version == grp.Max(x => x.GetName().Version))) // only select distinct assemblies based on FullName to avoid loading duplicate assemblies .Select(a => a.Location); var includeAssemblies = (IncludeAssemblies() ?? Enumerable.Empty<string>()); assemblies = assemblies.Concat(includeAssemblies) .Where(a => !string.IsNullOrWhiteSpace(a)) .Distinct(StringComparer.InvariantCultureIgnoreCase); @params.ReferencedAssemblies.AddRange(assemblies.ToArray()); string sourceCode = null; if (Debug) { var builder = new StringBuilder(); using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture)) { _codeDomProvider.GenerateCodeFromCompileUnit(compileUnit, writer, new CodeGeneratorOptions()); sourceCode = builder.ToString(); } } return Tuple.Create(_codeDomProvider.CompileAssemblyFromDom(@params, compileUnit), sourceCode); }
public override Tuple<Type, Assembly> CompileType(TypeContext context, ILogger logger) { if (context == null) throw new ArgumentNullException("context"); var result = Compile(context); var compileResult = result.Item1; if (compileResult.Errors != null && compileResult.Errors.Count > 0) { logger.Warn("The following template content will not compile:"); logger.Info(context.TemplateContent); foreach (var error in compileResult.Errors) { logger.Error(error.ToString().Split(':').Last().Trim(' ')); } throw new TemplateCompilationException(compileResult.Errors, result.Item2, context.TemplateContent); } return Tuple.Create( compileResult.CompiledAssembly.GetType("CompiledRazorTemplates.Dynamic." + context.ClassName), compileResult.CompiledAssembly); }
/// <summary> /// Compiles the type defined in the specified type context. /// </summary> /// <param name="context">The type context which defines the type to compile.</param> /// <param name="logger"></param> /// <returns>The compiled type.</returns> public abstract Tuple<Type, Assembly> CompileType(TypeContext context, ILogger logger);