-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSharpCompiler.cs
54 lines (42 loc) · 1.61 KB
/
CSharpCompiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Reflection;
namespace Goose
{
static class CSharpCompiler
{
public static Assembly Compile(string assemblyName, string[] src)
{
return Compile(assemblyName, src, null);
}
public static Assembly Compile(string outputPath, string[] src, params string[] referencedAssemblies)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters paras = new CompilerParameters() { OutputAssembly = outputPath };
if (referencedAssemblies != null)
{
foreach (string referencedAssembly in referencedAssemblies)
{
paras.ReferencedAssemblies.Add(referencedAssembly);
}
}
CompilerResults results = codeProvider.CompileAssemblyFromSource(paras,src);
if (results.Errors.Count > 0)
{
string msg = "";
foreach (CompilerError err in results.Errors)
{
msg = String.Concat(msg, err.ErrorNumber, ": ", err.ErrorText," @ ",
err.FileName , " L" , err.Line , " C" , err.Column);
}
throw new Exception("Compilation error(s)..." + msg);
}
return results.CompiledAssembly;
}
}
}