Exemplo n.º 1
0
        private async Task CreateExpressionEvaluator()
        {
            Options = ScriptOptions.Default;

            var assemblyLines = ReferencedAssembliesText.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in assemblyLines)
            {
                if (!line.StartsWith("//") && !string.IsNullOrWhiteSpace(line))
                {
                    var trimmed = line.Trim();

                    LOGGER.Info($"Attempting to load assembly: '{trimmed}'");

                    try
                    {
                        var assembly = Assembly.LoadFrom(trimmed);
                        Options = Options.AddReferences(assembly);

                        LOGGER.Info($"Loaded assembly: '{trimmed}' via 'Assembly.LoadFrom'");
                    }
                    catch (Exception ex1)
                    {
                        try
                        {
                            var netAssembly = Assembly.Load(trimmed);
                            Options = Options.AddReferences(netAssembly);

                            LOGGER.Info($"Loaded assembly: '{trimmed}' via 'Assembly.Load'");
                        }
                        catch (Exception ex2)
                        {
                            try
                            {
                                var gacAssemblyPath = AssemblyNameResolver.GetAssemblyPath(trimmed);
                                var gacAssembly     = Assembly.LoadFrom(gacAssemblyPath);
                                Options = Options.AddReferences(gacAssembly);

                                LOGGER.Info($"Loaded assembly: '{trimmed}' via 'AssemblyNameResolver.GetAssemblyPath'");
                            }
                            catch (Exception ex3)
                            {
                                LOGGER.Error($"Couldn't load assembly {trimmed} via 'Assembly.LoadFrom'", ex1);
                                LOGGER.Error($"Couldn't load assembly {trimmed} via 'Assembly.Load'", ex2);
                                LOGGER.Error($"Couldn't load assembly {trimmed} via 'AssemblyNameResolver.GetAssemblyPath'", ex3);
                            }
                        }
                    }
                }
            }

            CsScript = null;

            foreach (var func in StandardFunctions.GlobalFunctions)
            {
                LOGGER.Debug($"Loading standard function: '{func}'");

                if (CsScript == null)
                {
                    CsScript = await CSharpScript.RunAsync(func);
                }
                else
                {
                    CsScript = await CsScript.ContinueWithAsync(func);
                }
            }

            foreach (var var in GlobalVariables)
            {
                string func = $"const string {var.Key} = @\"{var.Value(FilePath, TemplateName)}\";";

                LOGGER.Info($"Loading global variable: '{func}'");

                CsScript = await CsScript.ContinueWithAsync(func);
            }

            try
            {
                LOGGER.Info($"Executing preparation script");
                LOGGER.Debug($"Preparation script: {CSharpPreparationScript}");

                CsScript = await CsScript.ContinueWithAsync(CSharpPreparationScript, Options);

                LOGGER.Info($"Executed preparation script");
            }
            catch (CompilationErrorException ex)
            {
                CsScript = await CSharpScript.RunAsync("using System;");

                LOGGER.Error($"Couldn't execute preparation script: {CSharpPreparationScript}", ex);
            }

            LOGGER.Info($"Expression evaluator creation finished");
        }