A)动态编译C#EXE和DLL是相对容易的.
B)执行EXE意味着运行新的应用程序.加载DLL意味着可以在可以在应用程序或项目之间共享的情况下使用方法和函数.
现在,可以从MSDN或为方便起见,找到编译EXE(或轻微修改,DLL)的最快最简单的方法:
private bool CompileCSharpCode(string script)
{
lvErrors.Items.Clear();
try
{
CSharpCodeProvider provider = new CSharpCodeProvider();
// Build the parameters for source compilation.
CompilerParameters cp = new CompilerParameters
{
GenerateInMemory = false,
GenerateExecutable = false, // True = EXE, False = DLL
IncludeDebugInformation = true,
OutputAssembly = "eventHandler.dll", // Compilation name
};
// Add in our included libs.
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
// Invoke compilation. This works from a string, but you can also load from a file using …Run Code Online (Sandbox Code Playgroud)