private static HostedCompiler GetCachedHostedCompiler(HashSet <Assembly> assemblySet) { if (HostedCompilerCache == null) { IEqualityComparer <HashSet <Assembly> > comparer = HashSet <Assembly> .CreateSetComparer(); HostedCompilerCache = new Dictionary <HashSet <Assembly>, HostedCompiler>(10, comparer); } lock (HostedCompilerCache) { HostedCompiler compiler; if (!HostedCompilerCache.TryGetValue(assemblySet, out compiler)) { if (HostedCompilerCache.Count >= 10) { HashSet <Assembly> key = HostedCompilerCache.Keys.ElementAtOrDefault <HashSet <Assembly> >(1); HostedCompilerCache.Remove(key); } compiler = new HostedCompiler(assemblySet.ToList <Assembly>()); HostedCompilerCache[assemblySet] = compiler; } return(compiler); } }
public Expression <Func <ActivityContext, T> > Compile <T>(LocationReferenceEnvironment environment) { CompilerResults results; Type type = typeof(T); this.environment = environment; if (this.referencedAssemblies == null) { this.referencedAssemblies = new HashSet <Assembly>(); } this.referencedAssemblies.UnionWith(DefaultReferencedAssemblies); List <Import> importList = new List <Import>(); foreach (string str in this.namespaceImports) { if (!string.IsNullOrEmpty(str)) { importList.Add(new Import(str)); } } HashSet <Type> typeReferences = null; EnsureTypeReferenced(type, ref typeReferences); foreach (Type type2 in typeReferences) { this.referencedAssemblies.Add(type2.Assembly); } VisualBasicScriptAndTypeScope scriptScope = new VisualBasicScriptAndTypeScope(this.environment, this.referencedAssemblies.ToList <Assembly>()); IImportScope importScope = new VisualBasicImportScope(importList); CompilerOptions options = new CompilerOptions { OptionStrict = OptionStrictSetting.On }; CompilerContext context = new CompilerContext(scriptScope, scriptScope, importScope, options); HostedCompiler cachedHostedCompiler = GetCachedHostedCompiler(this.referencedAssemblies); lock (cachedHostedCompiler) { try { results = cachedHostedCompiler.CompileExpression(this.textToCompile, context, type); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } FxTrace.Exception.TraceUnhandledException(exception); throw; } } if (scriptScope.ErrorMessage != null) { throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(this.textToCompile, scriptScope.ErrorMessage))); } if ((results.Errors != null) && (results.Errors.Count > 0)) { StringBuilder builder = new StringBuilder(); builder.AppendLine(); foreach (Microsoft.Compiler.VisualBasic.Error error in results.Errors) { builder.AppendLine(error.Description); } throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(this.textToCompile, builder.ToString()))); } LambdaExpression codeBlock = results.CodeBlock; if (codeBlock == null) { return(null); } Expression expression = this.Rewrite(codeBlock.Body, null); ParameterExpression[] parameters = new ParameterExpression[] { FindParameter(expression) ?? ExpressionUtilities.RuntimeContextParameter }; return(Expression.Lambda <Func <ActivityContext, T> >(expression, parameters)); }