private void Initialize()

in Forge.TreeWalker/src/ExpressionExecutor.cs [144:199]


        private void Initialize()
        {
            if (this.scriptCache.TryGetValue(ParentScriptCode, out this.parentScript))
            {
                // The parentScript is already initialized.
                this.ParentScriptTask = Task.CompletedTask;
                return;
            }

            this.ParentScriptTask = Task.Run(async () =>
            {
                ScriptOptions scriptOptions = ScriptOptions.Default;
#if NET462

                // MissingResolver improvement not available in NetStandard. Only add to Net 462.
                scriptOptions = ScriptOptions.Default.WithMetadataResolver(new MissingResolver());
#endif

                // Add references to required assemblies.
                Assembly mscorlib = typeof(object).Assembly;
                Assembly systemCore = typeof(System.Linq.Enumerable).Assembly;
                Assembly cSharpAssembly = typeof(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo).Assembly;
                scriptOptions = scriptOptions.AddReferences(mscorlib, systemCore, cSharpAssembly);

                // Add required namespaces.
                scriptOptions = scriptOptions.AddImports(
                    "System",
                    "System.Threading.Tasks");

                string systemCoreAssemblyName = mscorlib.GetName().Name;

                // Add external dependencies.
                if (this.dependencies != null)
                {
                    foreach (Type type in this.dependencies)
                    {
                        string fullAssemblyName = type.Assembly.GetName().Name;

                        // While adding the reference again is okay, we can not AddImports for systemCoreAssembly.
                        if (fullAssemblyName == systemCoreAssemblyName)
                        {
                            continue;
                        }

                        scriptOptions = scriptOptions.AddReferences(type.Assembly).AddImports(type.Namespace);
                    }
                }

                // Create the parentScript and add it to scriptCache. Execute the parentScript so Roslyn is primed to evaluate further expressions.
                this.parentScript = this.scriptCache.GetOrAdd(
                    ParentScriptCode,
                    (key) => CSharpScript.Create<object>(ParentScriptCode, scriptOptions, typeof(CodeGenInputParams)));

                await this.parentScript.RunAsync(this.parameters).ConfigureAwait(false);
            });
        }