2012-09-19 44 views
2

compliling代碼,我發現這個程序(http://support.microsoft.com/kb/304655),其中i編譯運行時的代碼,它適用於使用參考代碼,添加額外的參考,當在運行時

using System; 

以下是該程序的代碼在運行時,編譯代碼,

 CSharpCodeProvider codeProvider = new CSharpCodeProvider(); 
     ICodeCompiler icc = codeProvider.CreateCompiler(); 

     string Output = "Out.exe"; 
     Button ButtonObject = (Button)sender; 

     textBox2.Text = ""; 
     System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters(); 
     //Make sure we generate an EXE, not a DLL 
     parameters.GenerateExecutable = true; 
     parameters.OutputAssembly = Output; 
     CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text); 

     if (results.Errors.Count > 0) 
     { 
      textBox2.ForeColor = Color.Red; 
      foreach (CompilerError CompErr in results.Errors) 
      { 
       textBox2.Text = textBox2.Text + 
          "Line number " + CompErr.Line + 
          ", Error Number: " + CompErr.ErrorNumber + 
          ", '" + CompErr.ErrorText + ";" + 
          Environment.NewLine + Environment.NewLine; 
      } 
     } 
     else 
     { 
      //Successful Compile 
      textBox2.ForeColor = Color.Blue; 
      textBox2.Text = "Success!"; 
      //If we clicked run then launch our EXE 
      if (ButtonObject.Text == "Run") Process.Start(Output); 
     } 

而以下是我需要在運行時編譯代碼,

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Net; 
    using System.Text.RegularExpressions; 
    using Newtonsoft.Json.Linq; 

    namespace Tsubame 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"url"); 

       // Create Client 
       WebClient client = new WebClient(); 

       // Assign Credentials 
       client.Credentials = new NetworkCredential("user", "pass"); 

       //Grab Data 
       var data = client.DownloadString(@"url"); 

       JObject o = JObject.Parse(data); 
       string getFristRow = Convert.ToString(o["Body"][0]["RowId"]); 

       string encaplulateStart = "\\\""; 
       string encaplulateEnd = "\\\":"; 

       List<string> _matches = new List<string>(); 
       _matches = Regex.Matches(getFristRow, @"(?<=" + encaplulateStart + ").*(?=" + encaplulateEnd + ")") 
            .Cast<Match>() 
            .Select(m => m.Value) 
            .ToList(); 

       foreach (string head in _matches) 
       { 
        Console.WriteLine(head); 

       } 
       Console.ReadLine(); 
      } 
     } 
    } 

但是,當我輸入此給出了錯誤代碼,

Error Number: CS0234 

對於除系統之外的引用。請問我知道如何運行過程中添加額外的引用,以便它可以成功地編譯:)非常感謝你:)

回答

5

你需要使用CompilerParameters.ReferencedAssembliesCompilerParameters添加引用:

var parameters = CompilerParameters 
{ 
    GenerateExecutable = true, 
    OutputAssembly = Output, 
    ReferencedAssemblies = { 
     "System.dll", 
     "System.Core.dll", 
     // etc 
    } 
}; 

(當然你不必使用對象初始值設定語法來設置它,但它使得整潔的IMO。)

+0

非常感謝你!這是它:) –

+0

我用lyk parameters.ReferencedAssemblies.Add(「Newtonsoft.Json.dll」); 這和你的方法有什麼不同嗎? thanx :),並且var參數stub在代碼上嘗試時會給出錯誤,是否有任何原因?請原諒,如果它是一個簡單的解決方案,但我是新的這方編程:) –

+0

這是一樣的:) – abzarak