这个就是在程序中调用编译器···
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.Internal;
using Evaluator;
using System.Globalization;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace xmlPathTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddNodes_Click(object sender, EventArgs e)
{
// 1.CSharpCodePrivoder
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.ICodeComplier
ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
// 3.CompilerParameters
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("System.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Drawing.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Xml.dll");
objCompilerParameters.ReferencedAssemblies.Add("xmlPathTest.exe");
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
// 4.CompilerResults
CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, CreateCode(txtFrmName.Text.Trim()));
if (cr.Errors.HasErrors)
{
richTextBox1.Text = "编译错误:\r\n";
foreach (CompilerError err in cr.Errors)
{
richTextBox1.Text += err.ErrorText + "\r\n";
}
}
else
{
// 通过反射,调用HelloWorld的实例
Assembly objAssembly = cr.CompiledAssembly;
object objHelloWorld = objAssembly.CreateInstance("xmlPathTest.HelloWorld");
MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");
objMI.Invoke(objHelloWorld, null);
}
}
private string CreateCode(string frmName)
{
StringBuilder sb = new StringBuilder();
sb.Append("using System;");
sb.Append(Environment.NewLine);
sb.Append("using System.Collections.Generic;");
sb.Append(Environment.NewLine);
sb.Append("using System.ComponentModel;");
sb.Append(Environment.NewLine);
sb.Append("using System.Data;");
sb.Append(Environment.NewLine);
sb.Append("using System.Drawing;");
sb.Append(Environment.NewLine);
sb.Append("using System.Text;");
sb.Append(Environment.NewLine);
sb.Append("using System.Windows.Forms;");
sb.Append(Environment.NewLine);
sb.Append("namespace xmlPathTest");
sb.Append(Environment.NewLine);
sb.Append("{");
sb.Append(Environment.NewLine);
sb.Append(" public class HelloWorld");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.Append(" public void OutPut()");
sb.Append(Environment.NewLine);
sb.Append(" {");
sb.Append(Environment.NewLine);
sb.AppendFormat(" new xmlPathTest.{0}().Show();",frmName);
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append(" }");
sb.Append(Environment.NewLine);
sb.Append("}");
richTextBox1.Text = sb.ToString();
return sb.ToString();
}
}
}