1.
在写代码时注释文档
C#等.NET编程语言能够生成xml注释文档
然后可以利用sandcastle生成chm帮助文档
MSDN中推荐的文档注释:http://msdn.microsoft.com/en-us/library/5ast78ax.aspx
2.
可以使用VS自带的csc.exe生成XML文件,使用方法如下:
csc.exe /doc:Hello.xml /t:library Hello.cs
How to: Use the XML Documentation Features (C# Programming Guide)
3.
参考内容:
------------------------------------------------------------
1. http://www.cnblogs.com/netatomy/archive/2008/01/08/1031152.html
在编写代码时,为类型以及类型的成员添加文档注释是一个好的习惯。C#以及其他.NET语言的编译器能够将文档注释处理成一个XML文件,再利用一些工具(如Sandcastle和已经死去的NDoc),还能把文档注释制作成帮助文档。所以,有必要学习一下文档注释推荐使用的标记。
理论上,可以使用任意的标记,不过在MSDN中,推荐使用以下的文档注释标记:
- <c>
- <code>
- <example>
- <exception>
- <include>
- <list>
- <para>
- <param>
- <paramref>
- <permission>
- <remarks>
- <returns>
- <see>
- <seealso>
- <summary>
- <typeparam>
- <typepraramref>
- <value>
有些标记比较简单,可能已经被大家所熟悉和使用了,比如<summary>。而且如果使用了开发工具的话(如Visual Studio),还能自动生成标记以及属性,非常方便。
除了这些标记之外,其他语言,如VB.NET,和一些文档生成工具,如NDoc和Sandcastle等,还补充了一些标记:
- <event>
- <exclude>
- <filterpriority>
- <note>
- <overloads>
- <preliminary>
- <threadsafety>
这些补充的标记能够帮助文档生成工具在生成文档的时候,提供更丰富的内容。
参考资料:
2. http://www.cnblogs.com/netatomy/archive/2008/01/23/1050678.html
上篇文章介绍了 XML 文档注释的标记,这次演示一个例子,然后用 C# 编译器 csc.exe 生成 XML 文档注释的文件。
首先定义一个 Hello 类,其中包含一个构造函数和两个 ToString 方法,不管是类型还是成员,都加上了XML文档注释,内容如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
5
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
37
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
38
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
42
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
45
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
55
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
59
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
67
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
69
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
71
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
上面的代码使用了许多XML文档注释标记,其中大部分都是微软“建议的文档注释标记”,例如,<summary>、<see>、<remarks> 等,我也遵守了标准的使用方法;也有一些非建议的标记,例如,<threadsafety>、<overloads>,这些标记都被NDoc和/或Sandcastle所支持,所以也是有用的。
下面要做的事情,就是使用编译器csc.exe生成XML文件,使用方法如下:
csc.exe /doc:Hello.xml /t:library Hello.cs
回车后,csc 除了生成程序集之外,还会生成一个 Hello.xml 文件,该文件以XML的格式存储刚才编写的文档注释。csc 还会验证一些标记,以保证引用的类型或者成员是存在的,或者是没有歧义的,否则会出现警告。
如果使用 Visual Studio 则更加简单,只需要在项目属性窗口中指定文件名就可以了,每次生成时都会自动生成文档注释文件。
最终生成的文件内容如下:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们从文件中能发现这样几个特征:
- 文档描述了程序集和成员,分别用<assembly>标记和<members>标记表示。
- 针对每个类型和成员的文档注释,都被放在了相应的<member>标记中。
- 类型和类型成员都变成了完全限定名,并且都有一个前缀,类型使用“T”作为前缀,而成员使用“M”。结果 Hello 类变成了“T:Netatomy.Learning.Helo”,Hello.ToString(string) 则变成了“M:Netatomy.Learning.Hello.ToString(System.String)”。
- 构造函数的名称变成了“#ctor”。
- 除了上面的变化之外,输入的文档注释几乎原封不动地存到了文件中。
有了文档注释文件,我们接下来就可以使用 NDoc 或者 Sandcastle 这样的工具,生成 chm 帮助文档,或者 MS Help 2 格式的帮助文档,从而为我们的项目提供一个专业的 API 参考文档。
参考文档:
在上篇文章(编写并生成文档注释)中,我们已经了解了如何为类型和成员编写文档注释,以及如何生成相应的文档注释文件。在本文中,将简单地介绍如何使用 Sandcastle 生成 chm 文档。
Sandcastle 是一个文档生成工具,可以用它生成 MSDN 风格的文档,既能够生成 chm 文档,也能够生成 MS Help 2.x 帮助文档。在此之前曾流行的 NDoc,其作者已经放弃更新。
首先到 CodePlex 下载并安装 Sandcastle,目前最新版本是 Sandcastle January 2008 Release。安装后 Sandcastle 会创建一个系统环境变量 DXROOT,不要删除,因为 Sandcastle 要用这个环境变量。注意,如果之前安装过 Visual Studio 2005 SDK(安装它就意味着安装了早期版本的 Sandcastle),请删除用户环境变量 DXROOT,否则将影响新版本的使用。安装完成后,可以到安装目录下的 Examples 文件夹中看一看,这里有一些示例,可以用于研究 Sandcastle 的用法。
然后,准备好你的程序集和文档注释文件,我使用的是上篇文章中创建的 Hello.dll 和 Hello.xml。接着,到 Sandcastle 安装目录下,把 Examples\sandcastle 文件夹下的 build_sandcastle.bat 文件复制到你的文件夹下,和 Hello.dll 以及 Hello.xml 放在一起。
最后,打开命令行,进入到这个目录,输入:build_sandcastle.bat vs2005 Hello <回车>。后面会出现很多信息,等这个批处理程序结束后,能看到多出了许多文件和文件夹。如果中间没有出现问题的话,进入 chm 文件夹,将会看到一个 Hello.chm 文件,这就是我们最终想要得到的——帮助文档,打开看一看吧,是不是挺漂亮的。下面是截图: