日期:2014-05-17 浏览次数:20509 次
protected void Button1_Click(object sender, System.EventArgs e)
{
string s=@"using System;
public class test
{
public void HelloWorld()
{
}
}";
}
/// <summary>
/// 动态编译
/// </summary>
/// <param name="source"> 要动态编译的代码字符串</param>
/// <param name="path">生成dll文件的路径</param>
private void csc(string source, string path)
{
//-------------
}
using System.IO;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
ICodeCompiler complier = (new CSharpCodeProvider().CreateCompiler());
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
StreamReader sr = new StreamReader("d:\\test.cs");
CompilerResults cr = complier.CompileAssemblyFromSource(cp, "using System;\r\n" + sr.ReadToEnd());
sr.Close();
Assembly a = cr.CompiledAssembly;
object cls = a.CreateInstance("命名空间.类名称");
/// <summary>
/// 动态编译
/// </summary><