示例#1
0
        //--------------------------------------------------------------------------------------------------

        bool _Compile()
        {
            try
            {
                var codeStream = _ReadAndPreprocessFile(_ScriptInstance.Path);
                if (codeStream == null)
                {
                    return(false);
                }

                var baseDirectory = Path.GetDirectoryName(_ScriptInstance.Path);

                var metadataResolver = ScriptMetadataResolver.Default
                                       .WithSearchPaths(_MetadataSearchPaths)
                                       .WithBaseDirectory(baseDirectory);

                var sourceResolver = new ScriptSourceResolver(baseDirectory, _ReadAndPreprocessFile);

                var options = ScriptOptions.Default
                              .WithWarningLevel(4)
                              .WithReferences(_DefaultReferenceAppAssemblies)
                              .WithMetadataResolver(metadataResolver)
                              .WithSourceResolver(sourceResolver)
                              .WithImports(_DefaultImports)
                              .WithEmitDebugInformation(_EnableDebugging)
                              .WithLanguageVersion(LanguageVersion.Latest);

                var assemblyLoader = new InteractiveAssemblyLoader();
                foreach (var defaultAssembly in AssemblyLoadContext.Default.Assemblies)
                {
                    assemblyLoader.RegisterDependency(defaultAssembly);
                }
                // This extra reference is needed for unit test runner which isolate this assembly
                assemblyLoader.RegisterDependency(Assembly.GetExecutingAssembly());

                var script  = CSharpScript.Create(codeStream, options, _ScriptInstance.ContextInstance.GetType(), assemblyLoader);
                var results = script.Compile();
                codeStream.Dispose();

                var hasErrors = _ReportResults(results);
                if (!hasErrors)
                {
                    return(_ScriptInstance.Init(script.CreateDelegate()));
                }
            }
            catch (Exception e)
            {
                Messages.Exception("Script compilation failed. Scriptfile: " + _ScriptInstance.Path, e);
            }
            return(false);
        }
示例#2
0
        //--------------------------------------------------------------------------------------------------

        bool _Compile()
        {
            try
            {
                var codeStream = _ReadAndPreprocessFile(_ScriptInstance.Path);
                if (codeStream == null)
                {
                    return(false);
                }

                var baseDirectory = Path.GetDirectoryName(_ScriptInstance.Path);

                var metadataResolver = ScriptMetadataResolver.Default
                                       .WithSearchPaths(_MetadataSearchPaths)
                                       .WithBaseDirectory(baseDirectory);

                var sourceResolver = new ScriptSourceResolver(baseDirectory, _ReadAndPreprocessFile);

                var options = ScriptOptions.Default
                              .WithWarningLevel(4)
                              .WithReferences(_DefaultReferenceGacAssemblies)
                              .WithReferences(_DefaultReferenceAppAssemblies)
                              .WithMetadataResolver(metadataResolver)
                              .WithSourceResolver(sourceResolver)
                              .WithImports(_DefaultImports)
                              .WithEmitDebugInformation(_EnableDebugging)
                              .WithLanguageVersion(LanguageVersion.Latest);

                var script  = CSharpScript.Create(codeStream, options, _ScriptInstance.ContextInstance.GetType());
                var results = script.Compile();
                codeStream.Dispose();

                var hasErrors = _ReportResults(results);
                if (!hasErrors)
                {
                    return(_ScriptInstance.Init(script.CreateDelegate()));
                }
            }
            catch (Exception e)
            {
                Messages.Exception("Script compilation failed. Scriptfile: " + _ScriptInstance.Path, e);
            }
            return(false);
        }