小编Kom*_*k57的帖子

如何在运行,加载和使用中编译C#DLL

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)

c# runtime compilation appdomain .net-assembly

4
推荐指数
1
解决办法
8971
查看次数

标签 统计

.net-assembly ×1

appdomain ×1

c# ×1

compilation ×1

runtime ×1