2012-05-23 101 views
-1

我正在構建一個像Visual Studio中那樣的小即時窗口。我只是嘗試在Texttedit中編譯文本。現在我的問題:我如何進入我正在運行的程序的範圍?是否可以更改我的程序的值或列表? 這就是我目前的編譯代碼:簡單即時窗口

private void SimpleButtonCompileClick(object sender, EventArgs e) 
    { 
     CompilerParameters compilerParameters = new CompilerParameters(); 
     compilerParameters.GenerateExecutable = true; 
     compilerParameters.GenerateInMemory = true; 
     compilerParameters.TreatWarningsAsErrors = true; 
     compilerParameters.CompilerOptions = "/nowarn:1633"; 
     String sourceCode = ""; 
     string pattern = 
       "\\s*#pragma\\s+reference\\s+\"(?<path>[^\"]*)\"\\s*"; 
     System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(sourceCode, pattern); 
     if (match.Success) 
      compilerParameters.ReferencedAssemblies.Add(match.Groups["path"].Value); 
     // Specify .NET version 
     Dictionary<string, string> providerOptions = 
       new Dictionary<string, string>(); 
     providerOptions.Add("CompilerVersion", "v3.5"); 
     CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions); 
     // Compile source   
     CompilerResults results = provider.CompileAssemblyFromSource(compilerParameters, new string[] {sourceCode}); 
     // Show errors 
     if (results.Errors.HasErrors) 
     { 
      String errorString = ""; 
      foreach (var err in results.Errors) 
       errorString += err + "\n"; 
      XtraMessageBox.Show(errorString); 
     } 
     else 
     { 
      // Invoke compiled assembly's entry point 
      results.CompiledAssembly.EntryPoint.Invoke 
        (null, new object[0] {}); 
     } 
    } 

回答

0

我明白,你有辦法(?按鈕組合鍵),帶來了一個表單,您可以輸入您要編譯並執行代碼。由於Invoke是在相同的進程和AppDomain中執行的,只要您有一個訪問點,就可以訪問正在運行的程序中的所有內容。

例如,它可以是一個類中的公共靜態變量。或者,要獲得特定的表單,可以使用Application.OpenForms並通過FormCollection循環查找具有正確類型的表單。