日期:2008-07-27  浏览次数:20483 次

XML注释允许你在代码中添加结构化的文档,并且因为这是C#最先采用的技术,Visual Basic程序员觊觎已久了。XML注释能够描述各种各样的代码元素,包括类、字段成员、方法、以及枚举等等。


一旦你创建了一个描述代码特性的XML注释,当你继续在编辑器里键入代码特性时,你立刻会得到关于参数和返回值的智能感知。假设你现在有一个标志着如下签名的函数原型:

Private Function GetCustomerData(ByVal CustomerCode As String) As DataSet

把代码插入点设置在这个函数声明的上一行并键入三个小撇号。Visual Basic将生成一个和下面函数声明相匹配的XML注释,你可以像填充一个表格那样地填充它。根据你的函数,假设你将你的注释模板填充成如下内容:

''' <summary>
''' Returns a DataSet for a single customer.
''' </summary>
''' <param name="CustomerCode">Customer Code as String.</param>
''' <returns>DataSet for the supplied CustomerCode.</returns>
''' <remarks>Resides in the application's data layer.</remarks>

现在你无论在代码的何处键入这个函数名,你都会得到关于这个函数的概要、参数、和返回值的信息的完整智能感知,如图5所示:

图5 当你为你的代码元素添加了XML注释后,你可以立即得到其智能感知反馈,正如这个工具提示所展示的那样。

如果你是在集体协作下完成开发,这就是一个关键的特性。通过插入在源文件中的结构化的XML注释,你可以生成代码文档。并且,一个应用程序的架构师或开发组长可以在实现代码具体细节之前,先设计出原型并给出特性的注释。当开发人员在根据原型填写代码时,智能感知可以指导他们正确地使用到原型的特性。

编译器将把XML注释看作你代码的一部分。注释标签值显示成注释的颜色,而若你没有完整地规划出XML注释的话(作者意思是没有设计完整的函数原型,例如没有给出参数列表,涕淌注),只有summary标签值会显示在完整的XML注释模板上(见图6)。

图6 XML注释和编译器、代码窗已融为一体。这段关于FillDataGrid函数的XML注释回溃到了它的summary行。

如果你把函数原型中CustomerCode参数名改为CustomerID,编译器将在<param name="CustomerCode">下划上一条绿色的波浪线,警告你这两个参数名匹配错误(请参阅编译器警告部分获取更多信息)。

在你创建工程的时候,Visual Basic自动地生成一个XML文档文件。这个文件将以AssemblyName.XML为文件名出现在应用程序的输出目录下(文件主名是你的应用程序的名字,涕淌注)。因为这是个XML文件,所以你可以随心所欲地将它转换成其他的输出格式。XML注释极大地减少了为代码做文档的工作量,无论是你在输入代码时的智能感知,还是你创建工程后随即生成的代码文档。

@以下是原文供网友参考@

XML Comments

XML comments allow you to add structured documentation to your code, and have been the envy of Visual Basic developers because C# got them first. XML comments can describe a variety of code elements including classes, fields, methods, and enumerations.

Once you create an XML comment describing a code feature, you immediately get IntelliSense on parameters and the return type when you type the code feature in the editor. Assume you have a function prototype with the following signature:

Private Function GetCustomerData(ByVal CustomerCode As String) As DataSet

Place the code insertion point on the line above the function declaration and type three single-quote characters. Visual Basic generates an XML comment template matching the function declaration that you can tab through and fill out like a form. Assume you fill the comment template for your function like so:

''' <summary>
''' Returns a DataSet for a single customer.
''' </summary>
''' <param name="CustomerCode">Customer Code as String.</param>
''' <returns>DataSet for the supplied CustomerCode.</returns>
''' <remarks>Resides in the application's data layer.</remarks>

When you type this function name elsewhere in your code, you'll get full IntelliSense on the function summary, parameters, and return value as shown in Figure 5.

Figure 5. This tool tip shows the immediate IntelliSense feedback you get when you add XML Comments to your code elements.

This is a key feature in corporate and team environments. You can generate code documentation from the structured XML comments inserted in your source files. Also, an application architect or developer lead can prototype and comment features before implementation. As developers write code using the prototype objects, IntelliSense guides them in proper use of the features.

XML comments are treated as an integral part of your code by the compiler. The comment tag values are colored as comments, and if you collapse outlining on an XML comment, only the summary tag value displays in place of the whole XML comm