Inheriting Comments from an Interface in an Implementing Class

Inheriting comments from an interface in an implementing class?

GhostDoc does exactly that. For methods which aren't inherited, it tries to create a description out of the name.

FlingThing() becomes "Flings the Thing"

Inheriting XML comments from interfaces in C#

Updated Answer:

Use the <inheritdoc />-Tag.

Old Answer:

Linking XML Comments is IMHO not possible, but you could use a tool like GhostDoc to copy the XML Comment from your Interface/Baseclass to the implementation/derived class.

Can implementation classes inherit XML comments from their implemented interfaces?

This feature does not exist currently in Visual Studio.

You can get third party tools, such as GhostDoc that will help with creating XML documentation. GhostDoc also has the option to generate documentation that was "inherited" like you are attempting to do.

Link: GhostDoc

Ways to synchronize interface and implementation comments in C#

You can do this quite easily using the Microsoft Sandcastle (or NDoc) inheritdoc tag. It's not officially supported by the specification, but custom tags are perfectly acceptable, and indeed Microsoft chose to copy this (and one or two other tags) from NDoc when they created Sandcastle.

/// <inheritdoc/>
/// <remarks>
/// You can still specify all the normal XML tags here, and they will
/// overwrite inherited ones accordingly.
/// </remarks>
public void MethodImplementingInterfaceMethod(string foo, int bar)
{
//
}

Here is the help page from the Sandcastle Help File Builder GUI, which describes its usage in full.

(Of course, this isn't specifically "synchronisation", as your question mentions, but it would seem to be exactly what you're looking for nonetheless.)

As a note, this sounds like a perfectly fair idea to me, though I've observed that some people think you should always respecify comments in derived and implemented classes. (I've actually done it myself in documenting one of my libraries and I haven't see any problems whatsoever.) There's almost always no reason for the comments to differ at all, so why not just inherit and do it the easy way?

Edit: Regarding your update, Sandcastle can also take care of that for you. Sandcastle can output a modified version of the actual XML file it uses for input, which means you can distribute this modified version along with your library DLL instead of the one built directly by Visual Studio, which means you have the comments in intellisense as well as the documentation file (CHM, whatever).

Auto add ///inheritdoc/ when implementing methods from interface

Have you tried this option in ReSharper Options: ReSharper | Options | Code Editing | Members Generation | Generate documentation | Add <inheritdoc /> for overridden members? Sample Image

XML Documentation Comments with Interfaces and implementing class(es)

There seems to not be any support for such autodocumentation in Sandcastle. The Sandcastle Help File Builder though implements a custom inheritdoc tag.

From the SHFB site:

Support is included for the
<inheritdoc /> tag which allows you to
inherit documentation from base
types/members. This is implemented via
a standalone tool so it can also be
used by other third-party tools and
build scripts. This tool provides
features beyond those found in the
build component supplied with
Sandcastle.

Second Update: according to this workitem, the Sandcastle "support" for inheritdoc is through the SHFB tool. Bottom line I suppose is, SHFB solves your problem.

Comment the interface, implementation or both?

As a general rule, I use the same DRY (Don't Repeat Yourself) principle as with code:

  • on interface, document the interface
  • on implementation, document the implementation specifics

Java specific: when documenting the implementation, use {@inheritDoc} tag to "include" javadocs from the interface.

For more information:

  • Official javadoc documentation
  • Some unofficial advice.

Overriding a virtual method from within an inherited class but keep the comments of the base class

You could use ReSharper. Whenever you implement any interface it will bring over the XML comments as well (if you so choose).



Related Topics



Leave a reply



Submit