本文介绍如何使用 C# 编译器从文本源编译代码。
原始产品版本: Visual Studio、.NET Framework
原始 KB 数: 304655
总结
Microsoft .NET Framework 公开了允许以编程方式访问 C# 语言编译器的类。 如果要编写自己的代码编译实用工具,这可能很有用。 本文提供了用于从文本源编译代码的示例代码。 应用程序允许你仅生成可执行文件或生成可执行文件并运行它。 编译过程中发生的任何错误都显示在窗体上。
要求
- Visual Studio
- Visual C# 语言编译器
使用 C# 编译器编译代码
.NET Framework 提供 ICodeCompiler
编译器执行接口。 该 CSharpCodeProvider
类实现此接口,并提供对 C# 代码生成器和代码编译器实例的访问权限。 以下示例代码创建一个实例 CSharpCodeProvider
,并使用它来获取对接口的 ICodeCompiler
引用。
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
对接口的引用 ICodeCompiler
后,可以使用它编译源代码。 你将使用 CompilerParameters
类将参数传递给编译器。 下面是一个示例:
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results = icc.CompileAssemblyFromSource(parameters,SourceString);
上面的代码使用 CompilerParameters
对象告知编译器要生成可执行文件(而不是 DLL),并且希望将生成的程序集输出到磁盘。 对程序集的调用 CompileAssemblyFromSource
是编译程序集的位置。 此方法采用参数对象和源代码,这是一个字符串。 编译代码后,可以检查是否存在任何编译错误。 使用返回 CompileAssemblyFromSource
值,它是一个 CompilerResults
对象。 此对象包含一个错误集合,该集合包含编译期间发生的任何错误。
if (results.Errors.Count > 0)
{
foreach(CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
还有其他用于编译的选项,例如从文件进行编译。 还可以批量编译,这意味着可以同时编译多个文件或源。
分步过程示例
创建新的 Visual C# .NET Windows 应用程序。 Form1 默认创建。
将 Button 控件添加到 Form1,然后将其 Text 属性更改为 Build。
将另一个 Button 控件添加到 Form1,然后将其 Text 属性更改为“运行”。
将两个 TextBox 控件添加到 Form1,将这两个控件的多行属性设置为 True,然后调整这些控件的大小,以便可以将多行文本粘贴到每个控件中。
在代码编辑器中 ,打开Form1.cs 源文件。
在类中
Form1
,粘贴以下按钮单击处理程序。private void button1_Click(object sender, System.EventArgs e) { 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
语句:using System.CodeDom.Compiler; using System.Diagnostics; using Microsoft.CSharp;
在 Form1.cs中,找到
Form1
构造函数。调用
InitializeComponent
Form1
构造函数后,添加以下代码,将按钮单击处理程序连接到添加到 Form1 的两个按钮。public Form1() { InitializeComponent(); this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Click += new System.EventHandler(this.button1_Click); }
运行该项目。 加载 Form1 后,单击“生成”按钮。
注意
出现编译器错误。
接下来,将以下文本复制到文本框中,替换任何现有文本:
using System; namespace HelloWorld { /// <summary> /// Summary description for Class1. /// </summary> class HelloWorldClass { static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.ReadLine(); } } }
再次单击“ 生成 ”。 编译应成功。
单击“运行”,它将编译代码并运行生成的可执行文件。 编译将创建一个名为 Out.exe 的可执行文件,该文件与正在运行的应用程序保存在同一文件夹中。
注意
可以修改文本框中的代码以查看不同的编译器错误。 例如,删除其中一个分号并重新生成代码。
最后,修改文本框中的代码,将另一行文本输出到控制台窗口。 单击“运行”以查看输出。