我正在构建一个类似于 Visual Studio 中的即时窗口。我只是尝试在 Textedit 中编译文本。现在我的问题:我如何进入我正在运行的程序的范围?是否可以更改我的程序的值或列表?那是我当前的编译代码:
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] {});
}
}