定义
命名空间:
System.CodeDom.Compiler
程序集:
System.CodeDom.dll
为 CodeDomProvider 实现提供基类。 此类为抽象类。
本文内容
- 定义
- 示例
- 注解
- 实施者说明
- 构造函数
- 属性
- 方法
- 事件
- 适用于
- 另请参阅
CodeDomProviderpublic abstract class CodeDomProvider : System.ComponentModel.Component
派生
Microsoft.CSharp.CSharpCodeProvider
Microsoft.JScript.JScriptCodeProvider
Microsoft.VisualBasic.VBCodeProvider示例
以下示例程序可以根据使用类打印“Hello World”Console的程序的 CodeDOM 模型生成和编译源代码。 提供了Windows 窗体用户界面。 用户可以从多个选项中选择目标编程语言:C#、Visual Basic和JScript。 ```csharp using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Windows.Forms; using Microsoft.CSharp; using Microsoft.VisualBasic; using Microsoft.JScript;
// This example demonstrates building a Hello World program graph // using System.CodeDom elements. It calls code generator and // code compiler methods to build the program using CSharp, VB, or // JScript. A Windows Forms interface is included. Note: Code // must be compiled and linked with the Microsoft.JScript assembly. namespace CodeDOMExample { class CodeDomExample { // Build a Hello World program graph using // System.CodeDom types. public static CodeCompileUnit BuildHelloWorldGraph() { // Create a new CodeCompileUnit to contain // the program graph. CodeCompileUnit compileUnit = new CodeCompileUnit();
// Declare a new namespace called Samples.CodeNamespace samples = new CodeNamespace("Samples");// Add the new namespace to the compile unit.compileUnit.Namespaces.Add(samples);// Add the new namespace import for the System namespace.samples.Imports.Add(new CodeNamespaceImport("System"));// Declare a new type called Class1.CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");// Add the new type to the namespace type collection.samples.Types.Add(class1);// Declare a new code entry point method.CodeEntryPointMethod start = new CodeEntryPointMethod();// Create a type reference for the System.Console class.CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console");// Build a Console.WriteLine statement.CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(csSystemConsoleType, "WriteLine",new CodePrimitiveExpression("Hello World!"));// Add the WriteLine call to the statement collection.start.Statements.Add(cs1);// Build another Console.WriteLine statement.CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(csSystemConsoleType, "WriteLine",new CodePrimitiveExpression("Press the Enter key to continue."));// Add the WriteLine call to the statement collection.start.Statements.Add(cs2);// Build a call to System.Console.ReadLine.CodeMethodInvokeExpression csReadLine = new CodeMethodInvokeExpression(csSystemConsoleType, "ReadLine");// Add the ReadLine statement.start.Statements.Add(csReadLine);// Add the code entry point method to// the Members collection of the type.class1.Members.Add(start);return compileUnit;}public static void GenerateCode(CodeDomProvider provider,CodeCompileUnit compileunit){// Build the source file name with the appropriate// language extension.String sourceFile;if (provider.FileExtension[0] == '.'){sourceFile = "TestGraph" + provider.FileExtension;}else{sourceFile = "TestGraph." + provider.FileExtension;}// Create an IndentedTextWriter, constructed with// a StreamWriter to the source file.IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(sourceFile, false), " ");// Generate source code using the code generator.provider.GenerateCodeFromCompileUnit(compileunit, tw, new CodeGeneratorOptions());// Close the output file.tw.Close();}public static CompilerResults CompileCode(CodeDomProvider provider,String sourceFile,String exeFile){// Configure a CompilerParameters that links System.dll// and produces the specified executable file.String[] referenceAssemblies = { "System.dll" };CompilerParameters cp = new CompilerParameters(referenceAssemblies,exeFile, false);// Generate an executable rather than a DLL file.cp.GenerateExecutable = true;// Invoke compilation.CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);// Return the results of compilation.return cr;}}public class CodeDomExampleForm : System.Windows.Forms.Form{private System.Windows.Forms.Button run_button = new System.Windows.Forms.Button();private System.Windows.Forms.Button compile_button = new System.Windows.Forms.Button();private System.Windows.Forms.Button generate_button = new System.Windows.Forms.Button();private System.Windows.Forms.TextBox textBox1 = new System.Windows.Forms.TextBox();private System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox();private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();private void generate_button_Click(object sender, System.EventArgs e){CodeDomProvider provider = GetCurrentProvider();CodeDomExample.GenerateCode(provider, CodeDomExample.BuildHelloWorldGraph());// Build the source file name with the appropriate// language extension.String sourceFile;if (provider.FileExtension[0] == '.'){sourceFile = "TestGraph" + provider.FileExtension;}else{sourceFile = "TestGraph." + provider.FileExtension;}// Read in the generated source file and// display the source text.StreamReader sr = new StreamReader(sourceFile);textBox1.Text = sr.ReadToEnd();sr.Close();}private void compile_button_Click(object sender, System.EventArgs e){CodeDomProvider provider = GetCurrentProvider();// Build the source file name with the appropriate// language extension.String sourceFile;if (provider.FileExtension[0] == '.'){sourceFile = "TestGraph" + provider.FileExtension;}else{sourceFile = "TestGraph." + provider.FileExtension;}// Compile the source file into an executable output file.CompilerResults cr = CodeDomExample.CompileCode(provider,sourceFile,"TestGraph.exe");if (cr.Errors.Count > 0){// Display compilation errors.textBox1.Text = "Errors encountered while building " +sourceFile + " into " + cr.PathToAssembly + ": \r\n\n";foreach (CompilerError ce in cr.Errors)textBox1.AppendText(ce.ToString() + "\r\n");run_button.Enabled = false;}else{textBox1.Text = "Source " + sourceFile + " built into " +cr.PathToAssembly + " with no errors.";run_button.Enabled = true;}}private void run_button_Click(object sender,System.EventArgs e){Process.Start("TestGraph.exe");}private CodeDomProvider GetCurrentProvider(){CodeDomProvider provider;switch ((string)this.comboBox1.SelectedItem){case "CSharp":provider = CodeDomProvider.CreateProvider("CSharp");break;case "Visual Basic":provider = CodeDomProvider.CreateProvider("VisualBasic");break;case "JScript":provider = CodeDomProvider.CreateProvider("JScript");break;default:provider = CodeDomProvider.CreateProvider("CSharp");break;}return provider;}public CodeDomExampleForm(){this.SuspendLayout();// Set properties for label1this.label1.Location = new System.Drawing.Point(395, 20);this.label1.Size = new Size(180, 22);this.label1.Text = "Select a programming language:";// Set properties for comboBox1this.comboBox1.Location = new System.Drawing.Point(560, 16);this.comboBox1.Size = new Size(190, 23);this.comboBox1.Name = "comboBox1";this.comboBox1.Items.AddRange(new string[] { "CSharp", "Visual Basic", "JScript" });this.comboBox1.Anchor = System.Windows.Forms.AnchorStyles.Left| System.Windows.Forms.AnchorStyles.Right| System.Windows.Forms.AnchorStyles.Top;this.comboBox1.SelectedIndex = 0;// Set properties for generate_button.this.generate_button.Location = new System.Drawing.Point(8, 16);this.generate_button.Name = "generate_button";this.generate_button.Size = new System.Drawing.Size(120, 23);this.generate_button.Text = "Generate Code";this.generate_button.Click += new System.EventHandler(this.generate_button_Click);// Set properties for compile_button.this.compile_button.Location = new System.Drawing.Point(136, 16);this.compile_button.Name = "compile_button";this.compile_button.Size = new System.Drawing.Size(120, 23);this.compile_button.Text = "Compile";this.compile_button.Click += new System.EventHandler(this.compile_button_Click);// Set properties for run_button.this.run_button.Enabled = false;this.run_button.Location = new System.Drawing.Point(264, 16);this.run_button.Name = "run_button";this.run_button.Size = new System.Drawing.Size(120, 23);this.run_button.Text = "Run";this.run_button.Click += new System.EventHandler(this.run_button_Click);// Set properties for textBox1.this.textBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top| System.Windows.Forms.AnchorStyles.Bottom| System.Windows.Forms.AnchorStyles.Left| System.Windows.Forms.AnchorStyles.Right);this.textBox1.Location = new System.Drawing.Point(8, 48);this.textBox1.Multiline = true;this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(744, 280);this.textBox1.Text = "";// Set properties for the CodeDomExampleForm.this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(768, 340);this.MinimumSize = new System.Drawing.Size(750, 340);this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1,this.run_button, this.compile_button, this.generate_button,this.comboBox1, this.label1 });this.Name = "CodeDomExampleForm";this.Text = "CodeDom Hello World Example";this.ResumeLayout(false);}protected override void Dispose(bool disposing){base.Dispose(disposing);}[STAThread]static void Main(){Application.Run(new CodeDomExampleForm());}}
注解
A CodeDomProvider 可用于创建和检索代码生成器和代码编译器的实例。 代码生成器可用于以特定的语言生成代码,而代码编译器可用于将代码编译为程序集。
备注
在 .NET Framework 2.0 中,代码生成器和代码编译器中提供的方法直接从代码提供程序获取。 无需调用 CreateGenerator 或 CreateCompiler 访问方法,并且这些方法标记为已过时。 这适用于预先存在的代码提供程序实现和新的代码提供程序实现。
CodeDomProvider实现通常提供代码生成和/或代码编译接口,用于生成代码和管理单个编程语言的编译。 Windows SDK 附带的实现支持CodeDomProvider多种语言。 这些语言包括 C#、Visual Basic、C++ 和 JScript。 开发人员或编译器供应商可以实现 ICodeGenerator 和 ICodeCompiler 接口,并提供一个 CodeDomProvider 扩展 CodeDOM 支持的其他编程语言。
计算机配置文件 (Machine.config 中的 system.codedom> 元素) 为开发人员和编译器供应商提供了一种机制,用于为其他实现添加配置设置。< CodeDomProvider
该 CodeDomProvider 类提供静态方法来发现和枚举 CodeDomProvider 计算机上的实现。 该方法 GetAllCompilerInfo 返回计算机上的所有 CodeDomProvider 实现的设置。 该方法 GetCompilerInfo 根据编程语言名称返回特定 CodeDomProvider 实现的设置。 该方法 CreateProvider 返回特定语言实现的 CodeDomProvider 实例。
有关配置文件中的语言提供程序设置的更多详细信息,请参阅编译器和语言提供程序设置架构。
备注
此类在类级别发出链接需求和继承需求。 如果直接调用方或派生类没有完全信任权限,则会引发 A SecurityException 。 有关安全需求的详细信息,请参阅 链接需求 和 继承需求)。
实施者说明
在.NET Framework版本 1.0 和 1.1 中,代码提供程序包括实现CodeDomProvider、ICodeGenerator和ICodeParserICodeCompiler。 在 .NET Framework 2.0 中,和CreateGenerator()CreateParser()CreateCompiler()方法已过时,ICodeGeneratorICodeCompiler并且方法在类中CodeDomProvider直接可用。 应在代码提供程序实现中重写这些方法,而不是调用基方法。
构造函数
| CodeDomProvider() | 初始化 CodeDomProvider 类的新实例。 |
|---|---|
属性
| CanRaiseEvents | 获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
|---|---|
| Container | 获取包含 IContainer 的 Component。 (继承自 Component) |
| DesignMode | 获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
| Events | 获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
| FileExtension | 获取用于当前语言的源代码文件的默认文件扩展名。 |
| LanguageOptions | 获取语言功能标识符。 |
| Site | 获取或设置 Component 的 ISite。 (继承自 Component) |
方法
事件
| Disposed | 在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |
|---|---|
适用于
| 产品 | 版本 |
|---|---|
| .NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
| .NET Platform Extensions | 2.1, 2.2, 3.0, 3.1, 5, 6, 7 Preview 5 |
| Windows Desktop | 3.0, 3.1, 5, 6, 7 Preview 5 |
| Xamarin.Mac | 3.0 |
