private void btnLoad_Click(object sender, System.EventArgs e)
{
this.rtfCode.Text = "using System;" + System.Environment.NewLine;
this.rtfCode.Text += "using System.Collections;" + System.Environment.NewLine;
this.rtfCode.Text += "using System.Xml;" + System.Environment.NewLine;
this.rtfCode.Text += "using System.IO;" + System.Environment.NewLine;
this.rtfCode.Text += "using System.Windows.Forms;" + System.Environment.NewLine;
this.rtfCode.Text += System.Environment.NewLine;
this.rtfCode.Text += "namespace CSharpScripter" + System.Environment.NewLine;
this.rtfCode.Text += "{" + System.Environment.NewLine;
this.rtfCode.Text += " public class TestClass : CSharpScripter.Command" + System.Environment.NewLine;
this.rtfCode.Text += " {" + System.Environment.NewLine;
this.rtfCode.Text += " public TestClass()" + System.Environment.NewLine;
this.rtfCode.Text += " {" + System.Environment.NewLine;
this.rtfCode.Text += " }" + System.Environment.NewLine;
this.rtfCode.Text += System.Environment.NewLine;
this.rtfCode.Text += " public void Execute() " + System.Environment.NewLine;
this.rtfCode.Text += " {" + System.Environment.NewLine;
this.rtfCode.Text += " MessageBox.Show(\"This is a testmessage\");" + System.Environment.NewLine;
this.rtfCode.Text += " }" + System.Environment.NewLine;
this.rtfCode.Text += " }" + System.Environment.NewLine;
this.rtfCode.Text += "}" + System.Environment.NewLine;
}
private void btnCompile_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider csp = new CSharpCodeProvider();
ICodeCompiler cc = csp.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.OutputAssembly = Application.StartupPath + "\\TestClass.dll";
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Data.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("CSharpScripter.exe");
cp.WarningLevel = 3;
cp.CompilerOptions = "/target:library /optimize";
cp.GenerateExecutable = false;
cp.GenerateInMemory = false;
System.CodeDom.Compiler.TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false);
CompilerResults cr = new CompilerResults(tfc);
cr = cc.CompileAssemblyFromSource(cp, this.rtfCode.Text);
if (cr.Errors.Count > 0)
{
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText);
}
MessageBox.Show(this, "Errors occoured", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.btnExecute.Enabled = false;
}
else
{
this.btnExecute.Enabled = true;
}
System.Collections.Specialized.StringCollection sc = cr.Output;
foreach (string s in sc)
{
Console.WriteLine(s);
}
}
private void CheckErrors(CompilerErrorCollection cec)
{
}
private void btnExecute_Click(object sender, System.EventArgs e)
{
AppDomainSetup ads = new AppDomainSetup();
ads.ShadowCopyFiles = "true";
AppDomain.CurrentDomain.SetShadowCopyFiles();
AppDomain newDomain = AppDomain.CreateDomain("newDomain");
byte[] rawAssembly = loadFile("TestClass.dll");
Assembly assembly = newDomain.Load(rawAssembly, null);
Command testClass = (Command)assembly.CreateInstance("CSharpScripter.TestClass");
testClass.Execute();
testClass = null;
assembly = null;
AppDomain.Unload(newDomain);
newDomain = null;
}
private byte[] loadFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] buffer = new byte[(int) fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
fs = null;
return buffer;
}
//看完觉得可以用这些代码做很多有用的事情,不知道整个应用程序能不能这样加栽