.Net2.0在程序中编译代码
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
namespace CodeCompiler_Example
{
class Program
{
static string strCsCode = @"
class Cls
{
static void Main(string[] args)
{
global::System.Console.WriteLine(""HelloWorld!"");
global::System.Console.Read();
}
}
";
static void Main(string[] args)
{
try
{
CSharpCodeProvider provider = new CSharpCodeProvider();
// ICodeCompiler compiler = provider.CreateCompiler(); // For .Net FW 1.1
CompilerParameters parame = new CompilerParameters();
parame.OutputAssembly = @"C:\Cls.exe";
parame.GenerateExecutable = true;
provider.CompileAssemblyFromSource(parame, strCsCode);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Done.");
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
namespace CodeCompiler_Example
{
class Program
{
static string strCsCode = @"
class Cls
{
static void Main(string[] args)
{
global::System.Console.WriteLine(""HelloWorld!"");
global::System.Console.Read();
}
}
";
static void Main(string[] args)
{
try
{
CSharpCodeProvider provider = new CSharpCodeProvider();
// ICodeCompiler compiler = provider.CreateCompiler(); // For .Net FW 1.1
CompilerParameters parame = new CompilerParameters();
parame.OutputAssembly = @"C:\Cls.exe";
parame.GenerateExecutable = true;
provider.CompileAssemblyFromSource(parame, strCsCode);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Done.");
Console.Read();
}
}
}