NDoc: How to Make Good Use of Your XML Comments?

NDoc: How to Make Good Use of Your XML Comments?

Written by Allen Lee

Requirement

  • 使用C#进行库的开发。
  • 在源代码里使用XML注释。
  • 厌倦额外的文档编制工作,希望有无怨无悔的电脑接手这份枯燥无味的工作。
  • 具备C#的开发条件,至少需要.NET Framework SDK 1.1。
  • 能尽量抑制你的瞌睡虫,不让它阻碍你阅读本文。
  • 其它...

Encountered Problems

  • 面向对象技术的普及,为我们带来了极大的便利。对象的重用减轻了我们编码时许多不必要的痛苦,清晰的代码责任分离制免除了我们每顿都要面对着“意大利粉”的厌恶。
  • 但是,我们仍然要面对着枯燥的文档编制——从内容构思到页面排版。相信库开发人员对这种厌恶已经深有感触,即使我们都承认文档的重要性。 当然,某些大企业有专人写文档,但最了解库的却是程序员自己!而程序员往往又会在自己开发的库中为代码添加注释,如果有个工具能够利用这些注释按照一定的格式要求直接导出文档该多好呀!
  • 聪明而“懒惰”的程序员总是比其他人先行一步,他们不愿把自己放进重复而枯燥的方框内,他们千方百计要跳出这个方框,让自己能够从事更富创造性的活动,把这些枯燥无味的扔给无怨无悔的电脑们。

Feel Free to Write Your Class Library With XML Comments Added

  • 单纯的文字无法让你领略NDoc的魅力,至少我还没有这个能力。睡着了吗?(或许周公的故事更加有趣!)还没有的话请看看下面这段示范代码——一个表示复数的类(class)。
NDoc: How to Make Good Use of Your XML Comments?usingSystem;
NDoc: How to Make Good Use of Your XML Comments?
using
System.Globalization;
NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
namespace
MathUtils
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**/
///<summary>
NDoc: How to Make Good Use of Your XML Comments?
///提供复数的相关操作服务。
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?publicsealedclassComplex
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**/
///<summary>
NDoc: How to Make Good Use of Your XML Comments?
///默认构造器,将复数的实部与虚部分别设置为0;
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?publicComplex()
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?_Real
=0
;
NDoc: How to Make Good Use of Your XML Comments?_Imaginary
=0
;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**////<summary>
NDoc: How to Make Good Use of Your XML Comments?
///分别将复数的实部与虚部设置为指定的值。
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?
///<paramname="real"></param>
NDoc: How to Make Good Use of Your XML Comments?
///<paramname="imaginary"></param>

NDoc: How to Make Good Use of Your XML Comments?publicComplex(intreal,intimaginary)
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?_Real
=
real;
NDoc: How to Make Good Use of Your XML Comments?_Imaginary
=
imaginary;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
privateint_Real;
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**/
///<summary>
NDoc: How to Make Good Use of Your XML Comments?
///复数的实部。
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?publicintReal
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?
get

NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?{
NDoc: How to Make Good Use of Your XML Comments?
return
_Real;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
set
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?{
NDoc: How to Make Good Use of Your XML Comments?_Real
=
value;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
privateint_Imaginary;
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**/
///<summary>
NDoc: How to Make Good Use of Your XML Comments?
///复数的虚部。
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?publicintImaginary
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?
get

NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?{
NDoc: How to Make Good Use of Your XML Comments?
return
_Imaginary;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
set
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?{
NDoc: How to Make Good Use of Your XML Comments?_Imaginary
=
value;
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
/**////<summary>
NDoc: How to Make Good Use of Your XML Comments?
///把复数转换为对应的字符串形式。
NDoc: How to Make Good Use of Your XML Comments?
///</summary>

NDoc: How to Make Good Use of Your XML Comments?
///<returns>
NDoc: How to Make Good Use of Your XML Comments?
///返回(X)+(Y)i的形式的字符串。
NDoc: How to Make Good Use of Your XML Comments?
///</returns>

NDoc: How to Make Good Use of Your XML Comments?publicoverridestringToString()
NDoc: How to Make Good Use of Your XML Comments?NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?
{
NDoc: How to Make Good Use of Your XML Comments?
return
String.Format(CultureInfo.InvariantCulture,
NDoc: How to Make Good Use of Your XML Comments?
"{0}+{1}i"
,
NDoc: How to Make Good Use of Your XML Comments?_Real,
NDoc: How to Make Good Use of Your XML Comments?_Imaginary);
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?
NDoc: How to Make Good Use of Your XML Comments?}

NDoc: How to Make Good Use of Your XML Comments?}

Generate Your XML Comments to A Seperate XML File

  • 如果你有Visual C# .NET,那么你可以轻松的使用一下的步骤:在解决方案管理器中右键单击该项目图标,依次选择 属性 | 配置属性 | 生成,在 [输出] 的 [XML 文档文件] 右边填入目标XML文件的文件名,这里是Complex.xml。保存设置并关闭该对话框,生成该项目,你将会在项目的根目录中找到这个XML文件。

NDoc: How to Make Good Use of Your XML Comments?

  • 没有Visual C# .NET?没所谓,用C#的命令行编译器CSC.exe也可以。只需要在源代码所在的目录的命令提示符下键入csc /t:library/doc:Complex.xml Complex.cs<Enter>(详细请参见下面的评论),就可以生成XML注释文件了。
  • 好了,现在来看看我们第一阶段的劳动成果:
NDoc: How to Make Good Use of Your XML Comments?<?xmlversion="1.0"encoding="utf-8"?>
NDoc: How to Make Good Use of Your XML Comments?
<doc>

NDoc: How to Make Good Use of Your XML Comments?
<assembly>
NDoc: How to Make Good Use of Your XML Comments?
<name>Complex</name>
NDoc: How to Make Good Use of Your XML Comments?
</assembly>
NDoc: How to Make Good Use of Your XML Comments?
<members>
NDoc: How to Make Good Use of Your XML Comments?
<membername="T:MathUtils.Complex">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?提供复数的相关操作服务。
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
<membername="M:MathUtils.Complex.#ctor">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?默认构造器,将复数的实部与虚部分别设置为0;
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
<membername="M:MathUtils.Complex.#ctor(System.Int32,System.Int32)">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?分别将复数的实部与虚部设置为指定的值。
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
<paramname="real"></param>
NDoc: How to Make Good Use of Your XML Comments?
<paramname="imaginary"></param>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
<membername="M:MathUtils.Complex.ToString">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?把复数转换为对应的字符串形式。
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
<returns>
NDoc: How to Make Good Use of Your XML Comments?返回(X)+(Y)i的形式的字符串。
NDoc: How to Make Good Use of Your XML Comments?
</returns>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
<membername="P:MathUtils.Complex.Real">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?复数的实部。
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
<membername="P:MathUtils.Complex.Imaginary">
NDoc: How to Make Good Use of Your XML Comments?
<summary>
NDoc: How to Make Good Use of Your XML Comments?复数的虚部。
NDoc: How to Make Good Use of Your XML Comments?
</summary>
NDoc: How to Make Good Use of Your XML Comments?
</member>
NDoc: How to Make Good Use of Your XML Comments?
</members>
NDoc: How to Make Good Use of Your XML Comments?
</doc>

Where Is Our Protagonist?

  • 现在是时候轮到这出戏的主角出场了——NDoc。接下来...下载...安装...启动NDoc...(是否人类总不能摆脱枯燥无味呢?)按下 [Add] 来添加你的Dll或Exe,以及与之相关联的XML注释文件。

NDoc: How to Make Good Use of Your XML Comments?

  • 你希望NDoc用什么样的格式来生成库文档呢?哟,可供选择的类型也不少嘛!我们试试MSDN吧!

NDoc: How to Make Good Use of Your XML Comments?

  • 好了,万事俱备,只欠生成。Now build it!

NDoc: How to Make Good Use of Your XML Comments?

  • Well done! 这就是我们的第二阶段成果,也就是最终成果了!有没有觉得手痒想试一下呢?

NDoc: How to Make Good Use of Your XML Comments?

Conclusion

  • 终于大结局啦,一百个人就有一百个哈姆雷特,不知道NDoc在你心目中是怎么一个形象呢?
  • 不知道你是否认为NDoc偏心,只对能使用XML Comments的C#这么好。然而,VB.NET也不需要感到难过,因为你有个VBCommenter在背后默默的支持你呢!