using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
class
Program
{
static
void Main(string[] args)
{
var
csc =
new
CSharpCodeProvider(
new
Dictionary<string, string>() { {
"CompilerVersion"
,
"v3.5"
} });
var
parameters =
new
CompilerParameters(
new
[] {
"mscorlib.dll"
,
"System.Core.dll"
},
"foo.exe"
, true);
parameters.GenerateExecutable = true;
CompilerResults results = csc.CompileAssemblyFromSource(parameters,
@"using System.Linq;
class
Program {
public
static
void Main(string[] args) {
var
q = from i in Enumerable.Range(1,100)
where i % 2 == 0
select i;
foreach
(
var
item in q)
{
Console.WriteLine(item);
}
}
}");
results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
if
(results.Errors.
Count
== 0)
{
Assembly assembly = results.CompiledAssembly;
var
type = assembly.GetTypes()[0];
var
method = type.GetMethod(
"Main"
);
method.Invoke(null,
new
object[] {
new
string[] { } });
}
}
}