{"id":5674,"date":"2017-12-24T18:49:22","date_gmt":"2017-12-24T18:49:22","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5674"},"modified":"2017-12-24T18:49:22","modified_gmt":"2017-12-24T18:49:22","slug":"documenting-your-apis-using-xml-documentation","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/documenting-your-apis-using-xml-documentation\/","title":{"rendered":"Documenting your API&#8217;s using XML documentation"},"content":{"rendered":"<p>I&#8217;ve been updating one of my GitHub repositories (my Presentation.Core repo.) with some more documentation, using XML documentation comments, and thought I&#8217;d write a refresher post for taking your code from no documentation all the way through to being fully documented. <\/p>\n<p><strong>&lt;summary&gt;<\/strong><\/p>\n<p>So the \/\/\/ is used for XML documenting comments in C#, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ A Task\/async aware command object. \r\n\/\/\/ &lt;\/summary&gt;\r\npublic class AsyncCommand : CommandCommon\r\n{\r\n}\r\n<\/pre>\n<p>In the above, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/summary\" rel=\"noopener\" target=\"_blank\">&lt;summary&gt; element<\/a> (and it&#8217;s corresponding end element) within the \/\/\/ acts as a summary of our class. This class level summary is displayed in Visual Studio Intellisense when declaring a type of <em>AsyncCommand<\/em> when we create code such as <em>new AsyncCommand()<\/em> Intellisense will display the summary for the default constructor (if such documentation exists).<\/p>\n<p>The summary tag is the most important tag for documenting our code using the XML documentation as it&#8217;ll acts as the primary source of information for Intellisense and within generated help files. <\/p>\n<p><strong>&lt;remarks&gt;<\/strong><\/p>\n<p>This is an optional documentation <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/remarks\" rel=\"noopener\" target=\"_blank\">&lt;remarks&gt; element<\/a> which can be used to expand upon the summary or add supplementary documentation to the summary element. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ A Task\/async aware command object. \r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;remarks&gt;\r\n\/\/\/ Automatically handles changes to the built-in, IsBusy flag, so when\r\n\/\/\/ the command is executing the IsBusy is true and when completed or not \r\n\/\/\/ executing IsBusy is false.\r\n\/\/\/ &lt;\/remarks&gt;\r\npublic class AsyncCommand : CommandCommon\r\n{\r\n}\r\n<\/pre>\n<p>Summary text is displayed by IntelliSense whereas Remarks are shown in the help file output via tools such as Sandcastle.<\/p>\n<p><strong>&lt;returns&gt;<\/strong><\/p>\n<p>As you might guess, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/returns\" rel=\"noopener\" target=\"_blank\">&lt;returns&gt; element<\/a> is used on method return values, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Implementation of CanExecute which returns a boolean\r\n\/\/\/ to allow the calling method to determine whether the\r\n\/\/\/ Execute method can be called\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;parameter&quot;&gt;An object is ignored in this implementation&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;True if the command can be executed, otherwise False&lt;\/returns&gt;\r\npublic override bool CanExecute(object parameter)\r\n{\r\n}\r\n<\/pre>\n<p><strong>&lt;param&gt;<\/strong><\/p>\n<p>Continuing with the elements available for a method, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/param\" rel=\"noopener\" target=\"_blank\">param element<\/a> is used for method arguments, to allow the documentation to list what each parameter is used for.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Implementation of CanExecute which returns a boolean\r\n\/\/\/ to allow the calling method to determine whether the\r\n\/\/\/ Execute method can be called\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;parameter&quot;&gt;An object is ignored in this implementation&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;True if the command can be executed, otherwise False&lt;\/returns&gt;\r\npublic override bool CanExecute(object parameter)\r\n{\r\n}\r\n<\/pre>\n<p><strong>&lt;value&gt;<\/strong><\/p>\n<p>Like a return but used on a property, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/value\" rel=\"noopener\" target=\"_blank\">&lt;value&gt; element<\/a> is used along with a summary to describe what the property represents.<\/p>\n<p><strong>&lt;para&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/para\" rel=\"noopener\" target=\"_blank\">&lt;para&gt; element<\/a> is used within summary, remarks or return elements to add paragraph formatting\/structure to your documentation.<\/p>\n<p><strong>&lt;exception&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/exception\" rel=\"noopener\" target=\"_blank\">&lt;exception&gt; element<\/a> is used (as the name suggests) to list exceptions a method may throw during it&#8217;s execution. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Constructor take a comparison function which expects two types of T and\r\n\/\/\/ returns an integer where a value less than 0 indicates the first item is \r\n\/\/\/ before the second, a value of 0 indicates they're equivalent \r\n\/\/\/ and a value greater than 0 indicates the first item is after\r\n\/\/\/ the second item. This constructor also takes a function for the object \r\n\/\/\/ hash code method.\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;objectComparer&quot;&gt;\r\n\/\/\/ A function to compare two items of type T\r\n\/\/\/ &lt;\/param&gt;\r\n\/\/\/ &lt;exception cref=&quot;System.NullReferenceException&quot;&gt;\r\n\/\/\/ Thrown when the objectComparer is null\r\n\/\/\/ &lt;\/exception&gt;\r\npublic ComparerImpl(\r\n   Func&lt;T, T, int&gt; objectComparer)\r\n{\r\n   _objectComparer = \r\n      objectComparer ?? \r\n      throw new NullReferenceException(&quot;Comparer cannot be null&quot;);\r\n}\r\n<\/pre>\n<p>IntelliSense will display the list of exceptions<\/p>\n<p><strong>&lt;see&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/see\" rel=\"noopener\" target=\"_blank\">&lt;see&gt; element<\/a> allows us to reference documentation elsewhere within your code, it accepts a cref argument and within this we need to reference our classname.methodname etc. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Sets the property value against the property and raises\r\n\/\/\/ OnPropertyChanging, OnPropertyChanged etc. as required.\r\n\/\/\/ &lt;see cref=&quot;GetProperty{T}&quot;\/&gt;\r\n\/\/\/ &lt;\/summary&gt;\r\nprotected bool SetProperty&lt;T&gt;(\r\n   Func&lt;T&gt; getter, \r\n   Func&lt;T, T&gt; setter, \r\n   T value, \r\n   &#x5B;CallerMemberName] string propertyName = null)\r\n{\r\n}\r\n<\/pre>\n<p>Note: In the case of reference another method within the same class we need not declare the class name within the cref, but if referencing another class we should use classname.methodname syntax. <\/p>\n<p>Within IntelliSense and documentation the full method name will be displayed for the see element&#8217;s cref.<\/p>\n<p><strong>&lt;seealso&gt;<\/strong><\/p>\n<p>Usage for <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/seealso\" rel=\"noopener\" target=\"_blank\">&lt;seealso&gt;<\/a> is as per the see element but generated code will create a see also section and place such references within.<\/p>\n<p><strong>&lt;typeparam&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/typeparam\" rel=\"noopener\" target=\"_blank\">&lt;typeparam&gt; element<\/a> is used to document the generic parameters passed to a class and\/or methods, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Implements IComparer&amp;lt;T&amp;gt;, IEqualityComparer&amp;lt;T&amp;gt; \r\n\/\/\/ and IComparer\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;typeparam name=&quot;T&quot;&gt;The type of being compared&lt;\/typeparam&gt;\r\npublic class ComparerImpl&lt;T&gt; : \r\n   IComparer&lt;T&gt;, IEqualityComparer&lt;T&gt;, IComparer\r\n{\r\n}\r\n<\/pre>\n<p><strong>&lt;paramref&gt;<\/strong><\/p>\n<p>Used within a summary or remarks, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/paramref\" rel=\"noopener\" target=\"_blank\">&lt;paramref&gt;<\/a> is used to refer to a parameter on the method being documented. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Gets the current value via a Func via the &lt;paramref name=&quot;generateFunc&quot;\/&gt;\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;typeparam name=&quot;T&quot;&gt;The property type&lt;\/typeparam&gt;\r\n\/\/\/ &lt;param name=&quot;generateFunc&quot;&gt;The function to create the return value&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;propertyName&quot;&gt;The name of the property&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;The value of type T&lt;\/returns&gt;\r\nprotected T ReadOnlyProperty&lt;T&gt;(\r\n   Func&lt;T&gt; generateFunc, \r\n   &#x5B;CallerMemberName] string propertyName = null)\r\n{\r\n}\r\n<\/pre>\n<p>In the above, generateFunc is displayed within the IntelliSense summary and highlighted (via an italic font) in generated help files.<\/p>\n<p><strong>&lt;typeparamref&gt;<\/strong><\/p>\n<p>Like paramref, the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/typeparamref\" rel=\"noopener\" target=\"_blank\">&lt;typeparamref&gt; element<\/a> can be used within summary, remarks etc. to link to a specific generic type, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Gets the current property value as type &lt;typeparamref name=&quot;T&quot;\/&gt;\r\n\/\/\/ &lt;\/summary&gt;\r\nprotected T GetProperty&lt;T&gt;(\r\n   Func&lt;T&gt; getter, \r\n   Func&lt;T, T&gt; setter, \r\n   &#x5B;CallerMemberName] string propertyName = null)\r\n{\r\n}\r\n<\/pre>\n<p>As per paramref generated documentation may highlight the name within a help file and it&#8217;s also displayed via IntelliSense within the summary.<\/p>\n<p><strong>&lt;list&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/list\" rel=\"noopener\" target=\"_blank\">&lt;list&gt; element<\/a> allows us to define formatted text in a bullet, name or table format within our summary block, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Acts as a base class for view models, can be used\r\n\/\/\/ with backing fields or delegating getter\/setter \r\n\/\/\/ functionality to another class - useful for situations\r\n\/\/\/ where the underlying model is used directly\r\n\/\/\/ &lt;list type=&quot;bullet&quot;&gt;\r\n\/\/\/ &lt;item&gt;&lt;description&gt;GetProperty&lt;\/description&gt;&lt;\/item&gt;\r\n\/\/\/ &lt;item&gt;&lt;description&gt;SetProperty&lt;\/description&gt;&lt;\/item&gt;\r\n\/\/\/ &lt;item&gt;&lt;description&gt;ReadOnlyProperty&lt;\/description&gt;&lt;\/item&gt;\r\n\/\/\/ &lt;\/list&gt;\r\n\/\/\/ &lt;\/summary&gt;\r\npublic class ViewModelWithModel : ViewModelCommon\r\n{\r\n}\r\n<\/pre>\n<p>This results in generated documentation showing a bullet list of items, within IntelliSense, this isn&#8217;t quite so pretty and results in just the descriptions listed with space delimitation.<\/p>\n<p><strong>&lt;example&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/example\" rel=\"noopener\" target=\"_blank\">&lt;example&gt; element<\/a> allows us to add example code to our documentation, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Sets the property value against the property and raises\r\n\/\/\/ OnPropertyChanging, OnPropertyChanged etc. as required\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;example&gt;\r\n\/\/\/ &lt;code&gt;\r\n\/\/\/ public string Name\r\n\/\/\/ {\r\n\/\/\/    set =&gt; SetProperty(value);\r\n\/\/\/    get =&gt; GetProperty();\r\n\/\/\/ }\r\n\/\/\/ &lt;\/code&gt;\r\n\/\/\/ &lt;\/example&gt;\r\nprotected bool SetProperty&lt;T&gt;(\r\n   T value, \r\n   &#x5B;CallerMemberName] string propertyName = null)\r\n{\r\n}\r\n<\/pre>\n<p>Note: Without the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/code\" rel=\"noopener\" target=\"_blank\">&lt;code&gt; element<\/a> the example is not formatted as we&#8217;d expected. This is results in a generated help file section named examples.<\/p>\n<p><strong>&lt;code&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/code\" rel=\"noopener\" target=\"_blank\">&lt;code&gt; element<\/a> is used within the example element to format code samples. See above.<\/p>\n<p><strong>&lt;c&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/code-inline\" rel=\"noopener\" target=\"_blank\">&lt;c&gt; element<\/a> may be used within a summary or other element and formats the text within it as code. <\/p>\n<p><strong>&lt;permission&gt;<\/strong><\/p>\n<p>As you probably guessed, we can highlight the permissions expected or required for a method etc. using the <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/permission\" rel=\"noopener\" target=\"_blank\">&lt;permission&gt; element<\/a>. For example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Gets the current property value\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;permission cref=&quot;System.Security.PermissionSet&quot;&gt;\r\n\/\/\/ Unlimited access to this method.\r\n\/\/\/ &lt;\/permission&gt;\r\nprotected T GetProperty&lt;T&gt;(&#x5B;CallerMemberName] string propertyName = null)\r\n{\r\n}\r\n<\/pre>\n<p>The above will result in generated documentation with a security section which includes a table of the cref value and the description for the permissions required for the method.<\/p>\n<p><strong>&lt;include&gt;<\/strong><\/p>\n<p>The <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/xmldoc\/include\" rel=\"noopener\" target=\"_blank\">&lt;include&gt; element<\/a> allows us to use documentation from an external XML file.<\/p>\n<p><strong>How do we generate the XML documents?<\/strong><\/p>\n<p>Once we&#8217;ve documented our code using the XML documentation we will need to get Visual Studio to generate the XML files for the documents, basically extracting the comment blocks into these external files.<\/p>\n<p>For each project you wish to generate documentation for (within Visual Studio) select the properties for the project and then the Build tab. In the Output sections check the XML documentation file check box and either allow the default file name or create your own. <\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/codedoc\" rel=\"noopener\" target=\"_blank\">Documenting your code with XML comments<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been updating one of my GitHub repositories (my Presentation.Core repo.) with some more documentation, using XML documentation comments, and thought I&#8217;d write a refresher post for taking your code from no documentation all the way through to being fully documented. &lt;summary&gt; So the \/\/\/ is used for XML documenting comments in C#, for example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-5674","post","type-post","status-publish","format-standard","hentry","category-net"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5674"}],"version-history":[{"count":25,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5674\/revisions"}],"predecessor-version":[{"id":5699,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5674\/revisions\/5699"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}