Exemple #1
0
        public static Assembly Compile(CompilerInputData compilerInputData)
        {
            if (string.IsNullOrWhiteSpace(compilerInputData.CSharpCode))
            {
                throw new Exception("CSharpCode parameter can`t be null or empty.");
            }
            string assemblyName;

            if (string.IsNullOrWhiteSpace(compilerInputData.AssemblyName))
            {
                assemblyName = "CompiledAssembly_" + TextExtensions.Generate(10);
            }
            else
            {
                assemblyName = compilerInputData.AssemblyName;
            }
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(compilerInputData.CSharpCode);
            var        references = GetMetadataReferences(compilerInputData.ReferencedAssemblies);

            OptimizationLevel optLevel = OptimizationLevel.Release;

#if DEBUG
            //optLevel = OptimizationLevel.Debug;
#endif
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(
                    OutputKind.DynamicallyLinkedLibrary,
                    optimizationLevel: optLevel
                    )

                );

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);

                    string compileErrors = "";
                    foreach (Diagnostic diagnostic in failures)
                    {
                        compileErrors += $"\n  *{diagnostic.Id}: {diagnostic.GetMessage()};";
                    }
                    throw new CodeGenException("Exception while trying to compile script." + compileErrors);
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(ms.ToArray());
                    return(assembly);
                }
            }
        }
Exemple #2
0
        public CompilerInputData ToCompilerInputData(string assemblyName = null)
        {
            var compilerInputData = new CompilerInputData
            {
                CSharpCode           = CSharpCode,
                ReferencedAssemblies = Context.GetAssemblies(),
                AssemblyName         = assemblyName
            };

            return(compilerInputData);
        }