定义
命名空间:
System.CodeDom
程序集:
System.dll
初始化 CodeCommentStatement 类的新实例。
重载
| CodeCommentStatement() | 初始化 CodeCommentStatement 类的新实例。 |
|---|---|
| CodeCommentStatement(CodeComment)) | 使用指定的注释初始化 CodeCommentStatement 类的新实例。 |
| CodeCommentStatement(String)) | 使用指定文本作为内容来初始化 CodeCommentStatement 类的新实例。 |
| CodeCommentStatement(String, Boolean)) | 使用指定的文本和文档注释标志初始化 CodeCommentStatement 类的新实例。 |
CodeCommentStatement()
初始化 CodeCommentStatement 类的新实例。
C#复制
public CodeCommentStatement ();
适用于
CodeCommentStatement(CodeComment)
使用指定的注释初始化 CodeCommentStatement 类的新实例。
publicCodeCommentStatement (System.CodeDom.CodeComment comment);
参数
**comment** CodeComment
CodeComment,指示注释。
适用于
CodeCommentStatement(String)
使用指定文本作为内容来初始化 CodeCommentStatement 类的新实例。
public CodeCommentStatement (string text);
参数
**text** String
注释的内容。
适用于
CodeCommentStatement(String, Boolean)
使用指定的文本和文档注释标志初始化 CodeCommentStatement 类的新实例。
C#复制
public CodeCommentStatement (string text, bool docComment);
参数
**text** String
注释的内容。 **docComment** Boolean
当该注释是文档注释时为 **true**;否则为 **false**。
示例
下面的代码示例演示如何使用 CodeCommentStatement(String, Boolean)) 构造函数创建要用作 XML 注释字段的注释语句。 此示例是后面的较大示例的一部分。
// Declare a new code entry point method.CodeEntryPointMethod start = new CodeEntryPointMethod();start.Comments.Add(new CodeCommentStatement("<summary>", true));start.Comments.Add(new CodeCommentStatement("Main method for HelloWorld application.", true));start.Comments.Add(new CodeCommentStatement(@"<para>Add a new paragraph to the description.</para>", true));start.Comments.Add(new CodeCommentStatement("</summary>", true));
下面的代码示例演示如何创建简单的“Hello World”控制台应用程序和生成已编译应用程序的 XML 文档文件。
using System;using System.CodeDom;using System.CodeDom.Compiler;using System.IO;using System.Text.RegularExpressions;namespace BasicCodeDomApp{class Program{static string providerName = "cs";static string sourceFileName = "test.cs";static void Main(string[] args){CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);LogMessage("Building CodeDOM graph...");CodeCompileUnit cu = new CodeCompileUnit();cu = BuildHelloWorldGraph();StreamWriter sourceFile = new StreamWriter(sourceFileName);provider.GenerateCodeFromCompileUnit(cu, sourceFile, null);sourceFile.Close();CompilerParameters opt = new CompilerParameters(new string[]{"System.dll" });opt.GenerateExecutable = true;opt.OutputAssembly = "HelloWorld.exe";opt.TreatWarningsAsErrors = true;opt.IncludeDebugInformation = true;opt.GenerateInMemory = true;opt.CompilerOptions = "/doc:HelloWorldDoc.xml";CompilerResults results;LogMessage("Compiling with " + providerName);results = provider.CompileAssemblyFromFile(opt, sourceFileName);OutputResults(results);if (results.NativeCompilerReturnValue != 0){LogMessage("");LogMessage("Compilation failed.");}else{LogMessage("");LogMessage("Demo completed successfully.");}File.Delete(sourceFileName);}// 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");class1.Comments.Add(new CodeCommentStatement("<summary>", true));class1.Comments.Add(new CodeCommentStatement("Create a Hello World application.", true));class1.Comments.Add(new CodeCommentStatement("</summary>", true));class1.Comments.Add(new CodeCommentStatement(@"<seealso cref=" + '"' + "Class1.Main" + '"' + "/>", true));// Add the new type to the namespace type collection.samples.Types.Add(class1);// Declare a new code entry point method.CodeEntryPointMethod start = new CodeEntryPointMethod();start.Comments.Add(new CodeCommentStatement("<summary>", true));start.Comments.Add(new CodeCommentStatement("Main method for HelloWorld application.", true));start.Comments.Add(new CodeCommentStatement(@"<para>Add a new paragraph to the description.</para>", true));start.Comments.Add(new CodeCommentStatement("</summary>", true));// 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;}static void LogMessage(string text){Console.WriteLine(text);}static void OutputResults(CompilerResults results){LogMessage("NativeCompilerReturnValue=" +results.NativeCompilerReturnValue.ToString());foreach (string s in results.Output){LogMessage(s);}}}}
注解
如果参数 docComment为 true,则 CodeCommentStatement 为文档注释,注释使用三个分隔符字符进行结构化。 例如,在 C# 中,注释为///,在Visual Basic中'''。 文档注释用于标识 XML 注释字段,例如元素标识<summary> 的类型或成员摘要。
