83

I was wondering if anyone knows how to link an interface xml comment to an implementation. The problem is that I want the base comments to come from my interface first. Example:

interface myinterface {

       /// <summary>
       /// Does something.
       /// </summary>
       void method1(string foo);

}

and then the implementation is:

public class myclass : myinterface {


       public void method1(string foo) {
             //do something...
       }
}

So now if I hover over the method with my mouse after instantiating the object:

myclass foo = new myclass();
foo.method1("do something");

how can I make the comments appear in the hover popup? Is there some way I can link the interface comments to the implementation? I know there's a way in Java, but can't find the solution for C#.

Thanks

5
  • 15
    +1 for that's a damned good question ... Commented Sep 22, 2010 at 19:52
  • 3
    I've always wondered about this. There should be an attribute that you can add to a class to inherit documentation. Commented Jul 27, 2011 at 15:38
  • possible duplicate of Comment Inheritance for C# (actually any language) Commented Jan 8, 2014 at 18:15
  • What a weird omission from VS :/ Commented Jul 19, 2018 at 7:46
  • VS guys, can you please display summary from Interface when its not available on the actual method ! Commented Mar 26, 2019 at 11:47

6 Answers 6

21

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, use GhostDoc; it will first see if a parent class or method is overridden and copy the comment if so. ReSharper will also copy xml-doc comments in the more limited scope of extracting new superclasses/interfaces, or pushing members up/down in a hierarchy.
Doesn't this require the Pro version? I'm only able to do this with the non-Pro version by doing one member at a time, and it does not work at the class level.
ReSharper can do it too, so if you already own it, place the cursor over an inherited method/property, press ALT+RETURN and you can "Copy comments from base". ReSharper amazes me every day! :-)
///<inheritdoc/>. Use this in inplementation methods. Now you can over on "foo.method1("do something");" end read comments from interface
9

XMLDoc defines a tag <include /> for including comments from another file which has been around since Visual Studio 2003. The largest caveat is the referenced file should be a file containing only XMLDoc documentation, not another source file.

See the MSDN page for more details.

1 Comment

Not quite an answer but really useful information anyway, so thanks
7

If you use GhostDoc it helps a lot with "transporting" the documentation from interfaces to the implementing code.

4 Comments

Any tips on how this is accomplished (I have just installed ghost doc v4). Or is this a pro/free version thing?
@Konstantin I have always used the free version, and never done anything specific to have this happen. I just write the docs in the interface, and then when I press CTRL+D for a method in an implementing class (where the method has no docs yet), it is copied from the corresponding method in the interface. At least it used to work like that (not using GhostDoc in my current project).
Thanks seems to be a feature of the pro version these days.
6 years later, I just tried Ghostdoc Community (free) and it works. Write the doc in the interface and simply type /// above the method in the implementing class.
2

Looks like <inheritdoc/> will get native support soon.

See https://github.com/dotnet/csharplang/issues/313

Comments

0

http://blog.x-tensive.com/2008/02/fixml.html

It is a postprocessor that has certain additional options where the original documentation system is lacking.

From the website:

Brief summary:

FiXml is post-processor of XML documentation produced by C# \ Visual Basic.Net. It addresses some of the most annoying cases related to writing XML documentation in these languages: - No support for inheriting the documentation from base class or interface. I.e. a documentation for any overridden member should be written from scratch, although normally it’s quite desirable to inherit at least the part of it. - No support for insertion of commonly used documentation templates, such as “This type is singleton - use its property to get the only instance of it.”, or even “Initializes a new instance of class.”

1 Comment

Does anyone know if X-Tensive FiXml is meant to alter the source file or only the output XML docs? Oh - it's now found in a slightly different location as part of Xtensive.MSBuildTasks x-tensive.com/Downloads/?Path=Freeware\Xtensive.MSBuildTasks)
0

I built a command line tool to post-process the XML documentation files adding support for the <inheritdoc/> tag.

While it doesn't help with Intellisense in source code, it does allow the modified XML documentation files to be included in a NuGet package and therefore works with Intellisense in referenced NuGet packages.

See www.inheritdoc.io for more info (free version available).

1 Comment

I use this tag a lot, but I don't remember installing any 3rd party system to do so. Weird that no one else mentions it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.