C# 文档与注释

Introduction

我们中的大部分人都经历过不时地更新文档的痛苦。C# 和 Visual Studio .NET (VS.NET)使我们能够将代码和文档写在一个文件里,从而是维护文档的过程变得更加简单。 VS.NET通过从代码文件里提取特殊标记过的结构化的注释,并将他们生成到XML文件里做到这一点的。 这种 XML文件可以被用来生成人们可读(human-readable )的格式的文档,包括web页面,MSDN风格的帮助文档和编码窗口里的智能感应等。

Configure XML Commenting

为了使用这样的功能,你需要首先为你的VS.NET工程打开XML注释功能(XML commenting feature )。

  1. 在解决方案浏览器(solution explorer )里右击工程,选择属性"Properties".
  2. 在生成标签页里面你可以看到XML文档文件(XML Documentation File).打勾。这里你需要输入将来生成的XML文档文件的地址及文件名。你可以选择任意的文件名,但是为了与所有的XML注释功能保持兼容,文件名应当选择与程序集的名字一致。例如Adjuster.BusinessServices.dll 的XML文档文件名应当为Adjuster.BusinessServices.Xml

With this enabled, your XML comment data file will be rebuilt each time you build your project. Any problems that occur when trying to generate the file will not prevent a build but will be flagged in the VS.NET Task List. Assuming you do not have compile warnings set to errors.

 

VS.NET Task List flagging XML commenting error.

With that enabled you can start to use the special XML tags in your procedure �headers�. To get you started, place the cursor on the line directly above a procedure�s definition. Once there, press the �/� key three times, this will automatically insert a summary tag into your code. If the procedure had any arguments there should now be a param tag for each one.

The SaveData code above is what is inserted as default

This SaveData code is after I have added my comments describing what the routine does in the summary tag and what the data parameter is. This very simple action has given us enough to provide basic documentation including intellisense just like that provided by the .NET Framework assemblies.

It is clear from just this feature, how useful XML commenting is. When you include a reference to a .NET project that has XML commenting enabled, the XML documentation file we named earlier is copied over along with the binary to the current project�s /bin directory. This gives you the intellisense across assemblies.

The summary tag is the most basic of tags. The list below is the complete set currently supported by VS.NET. The ones marked with a * are the ones I feel are the most useful and the ones we will be dealing in the following examples.

  • c
    The c tag gives you a way to indicate that text within a description should be marked as code. Use code to indicate multiple lines as code.

  • code*
    The code tag gives you a way to indicate multiple lines as code. Use <c> to indicate that text within a description should be marked as code.

  • example*
    The example tag lets you specify an example of how to use a method or other library member. Commonly, this would involve use of the code tag.

  • exception*
    The exception tag lets you specify which exceptions a class can throw.

  • include
    The include tag lets you refer to comments in another file that describe the types and members in your source code. This is an alternative to placing documentation comments directly in your source code file.

  • para
    The para tag is for use inside a tag, such as <remarks> or <returns>, and lets you add structure to the text.

  • param*
    The param tag should be used in the comment for a method declaration to describe one of the parameters for the method.

  • paramref
    The paramref tag gives you a way to indicate that a word is a parameter. The XML file can be processed to format this parameter in some distinct way.

  • permission*
    The permission tag lets you document the access of a member. The System.Security.PermissionSet lets you specify access to a member.

  • remarks*
    The remarks tag is where you can specify overview information about a class or other type. <summary> is where you can describe the members of the type.

  • returns
    The returns tag should be used in the comment for a method declaration to describe the return value.

  • see
    The see tag lets you specify a link from within text. Use <seealso> to indicate text that you might want to appear in a See Also section.

  • seealso*
    The seealso tag lets you specify the text that you might want to appear in a See Also section. Use <see> to specify a link from within text.

  • summary*
    The summary tag should be used to describe a member for a type. Use <remarks> to supply information about the type itself.

  • value*
    The value tag lets you describe a property. Note that when you add a property via code wizard in the Visual Studio .NET development environment, it will add a <summary> tag for the new property. You should then manually add a <value> tag to describe the value that the property represents.

MSDN Style Documentation and NDOC

We have taken the intellisense format as far as it will go, but there is much more we can do with MSDN style documentation. There is a tool that comes with VS.NET that you will find at �Tools|Build Comment Web Pages�� which will take your C# XML comments from source files and generate linked HTML files. This comes straight out of the box so should not be totally disregarded. But if you want to create easy-to-use, helpful, cross-referenced and attractive documentation, then I can strongly recommend the free, open source tool NDoc. The screenshot below is taken from a compiled help file produced from NDoc and is an example of the quality it can produce.

The two routines below will show the correct usage for most of the XML comment tags we saw earlier. The cref attribute of the exception tag is used for cross-referencing to an Exception type. This attribute is also used in the seealso, permission and see tags to reference a type. The type must be available from the current compilation environment. The compiler checks that the referenced type exists and passes relevant data to the output XML.

 

This Age property once processed by NDoc will produce this.

I have drawn attention to areas in the picture and their corresponding XML comment tags.

Collapse Copy Code
 

This SaveData method once processed by NDoc will produce this

Again I have drawn attention to areas in the picture and their corresponding XML comment tags. The Accident cross-reference in the �See Also� section is the only one that I added. By default NDoc adds cross-referencing for the parent class, the parent class� members and the parent class� namespace.

With the combination of NDoc and VS.Net & C#�s ability to produce these comments you can get great technical documentation at a level so close to the code, that there is absolutely no excuse for it not telling it as it is.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Patrick Long


Member
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值