소스 안에 주석으로 XML comment를 추가하고, NDOC라는 오픈소스 프로젝트의 프로그램을 이용하여 자동으로 메뉴얼을 만들 수 있다.
MSDN 스타일의 뽀대나는 메뉴얼을 거져(?) 얻을 수 있다.
실제 적용할 만한 프로젝트가 있어 적용해 보고자 한다.
Introduction
Most of us will have experienced the dread of updating documentation at some point or other. C# and Visual Studio .NET (VS.NET) give us the ability to maintain code and documentation in the same file, which makes the whole process a lot easier. VS.NET does this by taking specially marked and structured comments from within the code and building them into an XML file. This XML file can then be used to generate human-readable documentation in a variety of forms including web pages, MSDN style documentation and Intellisense within the code window.
Configure XML Commenting
VS.NET produces XML comments by taking specially marked and structured comments from within the code and building them into an XML file. This XML file can then be used to generate human-readable documentation in a variety of forms including web pages, MSDN style documentation and Intellisense within the code window. The first thing you need to do is enable the XML commenting feature for your VS.NET project.
- Right Click on the project in the solution explorer and select "Properties".
- Within the properties dialog double click on the “Configuration Properties” node.
- The Build node should be already selected and you should be able to see the “XML Documentation File” entry under “Outputs”. Here is where you must enter the name of the XML file that will contain the comment data. You can call the file what you like, but for compatibility with all the features of XML commenting, it should take the form of MyAssemblyName.Xml e.g. Adjuster.BusinessServices.dll has a related XML file called 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.
/// <summary> /// /// </summary> /// <param name="data"></param> public void SaveData(ref DataSet data) { }
The SaveData
code above is what is inserted as default
/// <summary> /// Connects to the database and attempts to apply /// all adds, updates and deletes /// </summary> /// <param name="data">a dataset, passed by reference, /// that contains all the /// data for updating>/param> public void SaveData(ref DataSet data) { }
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
Thec
tag gives you a way to indicate that text within a description should be marked as code. Usecode
to indicate multiple lines as code.code
*
Thecode
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
*
Theexample
tag lets you specify an example of how to use a method or other library member. Commonly, this would involve use of thecode
tag.exception
*
Theexception
tag lets you specify which exceptions a class can throw.include
Theinclude
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
Thepara
tag is for use inside a tag, such as<remarks>
or<returns>
, and lets you add structure to the text.param
*
Theparam
tag should be used in the comment for a method declaration to describe one of the parameters for the method.paramref
Theparamref
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
*
Thepermission
tag lets you document the access of a member. TheSystem.Security.PermissionSet
lets you specify access to a member.remarks
*
Theremarks
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
Thereturns
tag should be used in the comment for a method declaration to describe the return value.see
Thesee
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
*
Theseealso
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
*
Thesummary
tag should be used to describe a member for a type. Use<remarks>
to supply information about the type itself.value
*
Thevalue
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.
/// <summary> /// Gets or sets the age of the person involved in the accident /// </summary> /// <value>Age of the claimant.</value> /// <remarks> The value must be numeric. /// </remarks> /// <exception cref="System.ApplicationException">Thrown when a non- /// numeric value is assigned.</exception> public string Age { }
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.
/// <summary> /// Connects to the database and attempts to apply all adds, /// updates and deletes /// </summary> /// <seealso cref="Adjuster.BusinessServices.Accident"/> /// <param name="data">a dataset, passed by reference, /// that contains all the /// data for updating</param> /// <example> This sample shows how to call the SaveData /// method from a wireless device. /// <code> /// ///AccidentCRUD accCRUD = new Adjuster.BusinessServices.AccidentCRUD(); ///accCRUD.SaveData(ref ds); /// ///</code> ///</example> ///<permission cref="System.Security.PermissionSet">Everyone ///can access this method.</Permission> public void SaveData(ref DataSet data) { }
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.
About Patrick Long
I have been programming now for 10 years starting with COBOL, CICS and DB2 and ending up with ASP.NET and C#. I am currently working for Charteris in London. We have a strong partnership with Microsoft UK and I my current project involves working as part of a Microsoft team. When I am not working i like playing tournament paintball. Click here to view Patrick Long's online profile. |
출처 : http://www.codeproject.com/csharp/csharpcommentinganddocs.asp
'programming > c#' 카테고리의 다른 글
[펌] C# Coding Guidelines (Microsoft) (3) | 2005.02.02 |
---|---|
Registry의 변경 정보 monitor하는 방법 (0) | 2005.01.23 |
tray에 icon과 context 메뉴 추가하는 방법 (0) | 2005.01.23 |