Automatic code documentation based on your C# comments
I’ve written a few APIs over the years and the worst part is writing the documentation:
- it takes extra time
- it must be updated every time you make changes to your code
- it is a duplication of work because I already document my code inline anyway
So, here’s a handy utility I wrote which will use reflection to whip through your code and draw out the comments.
Generating XML Documentation
Before proceeding, you must setup your project to generate an XML file of your code comments. This is done via the Properties –> Build menu in your project (presumably it’s a web project). See this screenshot below:
This generates an xml file in the bin directory every time you build. The file contains all your code comments, ready for parsing by my helper utility. The format is something like this:
So, with this in place, here is the utility class:
Note that this won’t compile for you because it references a custom attribute, ApiMethodAttribute, and my base class, BaseController. However, you could delete the logic around these and the documentation should still generate.
Now it’s just a matter of calling the class. I use mine in an MVC ActionResult:
And for clarity, I’ll include my View, so you can see how it’s used to render the results to the user:
Hope that helps.