Winform 코드 편집기 - 문자열을 dll로 컴파일합니다.

 using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;//    
using System.Web.Hosting;

CSharpCodeProvider comp = new CSharpCodeProvider();
        CompilerParameters cp = new CompilerParameters();
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("system.data.dll");
        cp.ReferencedAssemblies.Add(HostingEnvironment.MapPath("~/") + "bin//TEST.DBUtility.dll");
        cp.ReferencedAssemblies.Add("System.Xml.dll");
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = false;
        CompilerResults _Result = comp.CompileAssemblyFromSource(cp, _sb.ToString());\
        if (_Result.Errors.Count == 0)
            {
                return "    !";
            }

코드 편집 상자:
Fireball.Windows.Forms.CodeEditorControl : Fireball.CodeEditor 컨트롤내 리소스
구체적으로 다음을 볼 수 있습니다.http://msdn.microsoft.com/zh-cn/library/microsoft.csharp.csharpcodeprovider(v=vs.80).aspx
public static bool CompileExecutable(String sourceName)
{
    FileInfo sourceFile = new FileInfo(sourceName);
    CodeDomProvider provider = null;
    bool compileOk = false;

    // Select the code provider based on the input file extension.
    if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
    {
        provider = new Microsoft.CSharp.CSharpCodeProvider();
    }
    else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
    {
        provider = new Microsoft.VisualBasic.VBCodeProvider();
    }
    else 
    {
        Console.WriteLine("Source file must have a .cs or .vb extension");
    }

    if (provider != null)
    {

        // Format the executable file name.
        // Build the output assembly path using the current directory
        // and <source>_cs.exe or <source>_vb.exe.
 
        String exeName = String.Format(@"{0}\{1}.exe", 
            System.Environment.CurrentDirectory, 
            sourceFile.Name.Replace(".", "_"));

        CompilerParameters cp = new CompilerParameters();

        // Generate an executable instead of 
        // a class library.
        cp.GenerateExecutable = true;

        // Specify the assembly file name to generate.
        cp.OutputAssembly = exeName;
    
        // Save the assembly as a physical file.
        cp.GenerateInMemory = false;
    
        // Set whether to treat all warnings as errors.
        cp.TreatWarningsAsErrors = false;
 
        // Invoke compilation of the source file.
        CompilerResults cr = provider.CompileAssemblyFromFile(cp, 
            sourceName);
    
        if(cr.Errors.Count > 0)
        {
            // Display compilation errors.
            Console.WriteLine("Errors building {0} into {1}",  
                sourceName, cr.PathToAssembly);
            foreach(CompilerError ce in cr.Errors)
            {
                Console.WriteLine("  {0}", ce.ToString());
                Console.WriteLine();
            }
        }
        else
        {
            // Display a successful compilation message.
            Console.WriteLine("Source {0} built into {1} successfully.",
                sourceName, cr.PathToAssembly);
        }
      
        // Return the results of the compilation.
        if (cr.Errors.Count > 0)
        {
            compileOk = false;
        }
        else 
        {
            compileOk = true;
        }
    }
    return compileOk;
}

좋은 웹페이지 즐겨찾기