コード例 #1
0
        public static void CheckForSyntaxErrors(string code)
        {
            SyntaxTree methodTree  = CSharpSyntaxTree.ParseText(code);
            var        diagnostics = methodTree.GetDiagnostics();

            if (diagnostics.Any(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error))
            {
                throw new ScriptCompilerException(ScriptUtility.FormatCompilationError(diagnostics, true));
            }
        }
コード例 #2
0
        Assembly EmitIL(Compilation compilation, string writeToDllOptional)
        {
            var emitInMemory = string.IsNullOrEmpty(writeToDllOptional);

            using (var peStream = new MemoryStream())
                using (var pdbStream = new MemoryStream())
                {
                    var emitOptions = new EmitOptions(false, DebugInformationFormat.PortablePdb);

                    EmitResult result;
                    if (DebugBuild)
                    {
                        result = compilation.Emit(peStream, pdbStream, options: emitOptions);
                    }
                    else
                    {
                        result = compilation.Emit(peStream);
                    }

                    if (result.Success)
                    {
                        if (emitInMemory)
                        {
                            //use _assemblyLoader.LoadAssemblyFromStream see RoslynPad
                            peStream.Seek(0, SeekOrigin.Begin);
                            if (DebugBuild)
                            {
                                pdbStream.Seek(0, SeekOrigin.Begin);
                                return(AppDomain.CurrentDomain.Load(peStream.GetBuffer(), pdbStream.GetBuffer()));
                            }

                            return(AppDomain.CurrentDomain.Load(peStream.GetBuffer()));
                        }
                        else
                        {
                            peStream.Seek(0, SeekOrigin.Begin);
                            CopyToFile(writeToDllOptional, peStream);
                            //CopyToFileAsync( assemblyPath, peStream ).Wait();

                            if (DebugBuild)
                            {
                                pdbStream.Seek(0, SeekOrigin.Begin);
                                CopyToFile(Path.ChangeExtension(writeToDllOptional, "pdb"), pdbStream);
                                //CopyToFileAsync( Path.ChangeExtension( assemblyPath, "pdb" ), pdbStream ).ConfigureAwait( false );
                            }

                            return(Assembly.LoadFrom(writeToDllOptional));
                        }
                    }
                    else
                    {
                        throw new ScriptCompilerException(ScriptUtility.FormatCompilationError(result.Diagnostics, DebugBuild));
                    }
                }
        }