2012-04-16 1 views
0

주어진 C# 코드를 텍스트 상자에 컴파일 한 다음 exe에 저장할 수 있습니까?텍스트 상자에 코드를 컴파일하고 exe에 저장

이것이 가능합니까? 그렇다면 어떻게 할 수 있습니까?

+2

이 주제에 대한 연구를 직접 수행 했습니까? 그렇다면 무엇을 발견 했습니까? –

+0

그래, 나는 단지 런타임 컴파일을 발견했지만 저장하지 않았다. –

+0

당신은 여기를 보셨습니까 http://www.codeproject.com/Articles/9019/Compiling-and-Executing-Code-at-Runtime –

답변

4

예, 가능합니다. CodeDOM을 사용할 수 있습니다. 그리고 여기 an example입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.CSharp; 
using System.CodeDom.Compiler; 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } }); 
     var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true); 
     parameters.GenerateExecutable = true; 
     CompilerResults results = csc.CompileAssemblyFromSource(parameters, 
     @"using System.Linq; 
      class Program { 
       public static void Main(string[] args) { 
       var q = from i in Enumerable.Rnge(1,100) 
          where i % 2 == 0 
          select i; 
       } 
      }"); 
     results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText)); 
    } 
} 
+0

감사합니다. 정말 도움이되었습니다. –

0

는 별개로 런타임에 코드를 컴파일에서, 당신은 단지 그것을 컴파일 csc.exe를 사용하여 다음 디스크로 텍스트 상자에서 코드를 저장하고 있습니다. 명령은 다음과 유사하게 나타납니다.

%systemroot%\Microsoft.NET\Framework\v3.5\csc /out:filename.exe filename.cs 
관련 문제