2013-06-15 1 views
0

에서 "사용 전 컴파일 될 소스 코드를이 라인에 추가 할 방법 :가 된 CodeDom 소스 코드

Temp.AppendLine(@"[assembly: AssemblyDescription("[DESCRIPTION]"]);"); 

을하지만 당신은 [설명]을 참조 너무 문자열에없는 ... 내가 그렇게 할 수있는 방법 내가 텍스트 상자의 입력을 사용하려면 대신 [DISCRIPTION]의 .?

 private void button1_Click(object sender, EventArgs e) 
    { 
     SaveFileDialog d = new SaveFileDialog(); 
     d.Filter = "Executable (*.exe)|*.exe"; 
     if (d.ShowDialog() == DialogResult.OK) 
     { 

      String InputCode = String.Empty; 
      String regcode = String.Empty; 
      //Unser TestCode, in dem Wir ein MessageBox aufrufen 
      InputCode = "MessageBox.Show((1 + 2 + 3).ToString());"; 

      System.CodeDom.Compiler.CodeDomProvider CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp"); 
      //Parameter für die Compilierung, wie die einzubindenen Bibliotheken usw. 
      System.CodeDom.Compiler.CompilerParameters CompilerParameters = new System.CodeDom.Compiler.CompilerParameters(); 
      CompilerParameters.ReferencedAssemblies.Add("System.dll"); 
      CompilerParameters.OutputAssembly = d.FileName; 
      CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); 
      CompilerParameters.CompilerOptions += "/target:winexe" + " " + "/win32icon:" + "\"" + textBox6.Text + "\""; 
      CompilerParameters.GenerateExecutable = true; 
      CompilerParameters.GenerateInMemory = false; 

      //Über den StringBuilder wird der Code zusammengesetzt 
      StringBuilder Temp = new StringBuilder(); 
      Temp.AppendLine(@"using System;"); 
      Temp.AppendLine(@"using System.Windows.Forms;"); 
      Temp.AppendLine(@"using System.Diagnostics;"); 
      Temp.AppendLine(@"namespace RunTimeCompiler{"); 
      Temp.AppendLine(@"public class Test{"); 
      Temp.AppendLine(@"public static void Main(){"); 

      Temp.AppendLine(@InputCode); 
      Temp.AppendLine(@"}"); 
      Temp.AppendLine(@"public void Ergebnis(){"); 
      Temp.AppendLine(@"}}}"); 


      //Compilieren 
      System.CodeDom.Compiler.CompilerResults CompilerResults = CodeDomProvider.CompileAssemblyFromSource(CompilerParameters, Temp.ToString()); 
      //Auf CompilerFehler prüfen 
      if (CompilerResults.Errors.Count > 0) 
      { 
       MessageBox.Show(CompilerResults.Errors[0].ErrorText, "Fehler bei Laufzeitkompilierung", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return; 
      } 
      else 
      { 
       MessageBox.Show("Done", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 

답변

0

는 텍스트 상자에 입력을 문자 그대로 C#을 문자열로 변환이 포함 된 문자열 내 놓습니다 속성 코드 :

string description = textBox1.Text; 

string descriptionAsCsharpStringLiteral = 
    "@\"" + description.Replace("\"", "\"\"") + "\""; 

string attribute = 
    "[assembly: AssemblyDescription(" + descriptionAsCsharpStringLiteral + ")]"; 

Temp.AppendLine("using System.Reflection;") 
    .AppendLine(attribute) 
    .AppendLine("namespace ..."); 
+0

어셈블리가이 선언에 유효한 위치가 아닙니다.이 선언의 유효한 위치는 "type"입니다. –

+0

최상위 레벨의''AssemblyDescription'' 다음에'AssemblyDescription'과 같은 전역 속성이 소스 코드에 나타나야합니다. '지시어를 사용하고 어떤 타입이나 네임 스페이스 선언을하기 전에. [전역 특성 (C# 프로그래밍 가이드)] (http://msdn.microsoft.com/en-us/library/284c1c4s(v=9090).aspx) – dtb

+0

당신이 말한 것처럼, 지금이 오류가 발생했습니다 : 형식 또는 NameSpace 이름 "AssemblyDescription"을 찾을 수 없습니다 (사용 지시문이나 어셈블리 참조가 누락 되었습니까?) –