简体   繁体   English

使用相同的 XML 注释记录重载方法

[英]Documenting overloaded methods with the same XML comments

Say I have this constructor:说我有这个构造函数:

/// <summary>
/// Example comment.
/// </summary>
public SftpConnection(string host, string username, 
    string password, int port) {...}

which has these overloads:有这些重载:

public SftpConnection(string host, string username, string password) 
    : this(host, username, password, 22) { }

public SftpConnection(string host, string username, int port) 
    : this(host, username, "", port) { }

public SftpConnection(string host, string username) 
    : this(host, username, "", 22) { }

and in reality, the XML comment is pretty large, with param , example and exception elements and so on.实际上,XML 注释非常大,包含paramexampleexception元素等等。

Is there some way to add a special XML comment one-liner to the overloads, such that they use the exact same comments so that I don't need to copy-paste the whole, enormous original comments?有没有办法向重载添加一个特殊的 XML 注释单行,以便它们使用完全相同的注释,这样我就不需要复制粘贴整个大量的原始注释?

I'm thinking something like: <use cref="SftpConnection(string,string,string,int)" /> which doesn't work of course.我在想这样的事情: <use cref="SftpConnection(string,string,string,int)" />这当然不起作用。

I am aware of the include element, but I get the impression it reads the comments from an XML file instead, which I don't want - I want the comment to still be visible in the code, but only once .我知道include元素,但我的印象是它从 XML 文件读取注释,这是我不想要的 - 我希望注释在代码中仍然可见,但只有一次

Thanks :-)谢谢:-)

You can't really do this.你真的不能这样做。 I find it annoying too.我也觉得很烦。

However, you can alleviate the problem by using default parameter values instead of lots of overloads.但是,您可以通过使用默认参数值而不是大量重载来缓解该问题。 Instead of:而不是:

public SftpConnection(string host, string username, string password, int port)
public SftpConnection(string host, string username, string password)
public SftpConnection(string host, string username, int port)
public SftpConnection(string host, string username)

You can have just a single one:你可以只有一个:

public SftpConnection(string host, string username, string password = "",
                      int port = 22)

This has multiple advantages:这有多个优点:

  • Need only one XML comment.只需要一个 XML 注释。 The whole point of my answer.我的回答的全部要点。

  • Users of Visual Studio can instantly see that the default value for port is 22. With the overloads, this is not obvious; Visual Studio 的用户可以立即看到port的默认值是 22。对于重载,这并不明显; you have to specifically mention it in the documentation.您必须在文档中特别提及它。

  • You indirectly encourage client code to become more readable by encouraging the use of named parameters (eg port: 2222 instead of just 2222 , which is less clear).通过鼓励使用命名参数(例如port: 2222而不是仅仅2222 ,这不太清楚),您间接地鼓励客户端代码变得更具可读性。

And the greatest part about this is that using default values does not remove the ability to still have several overloads if you need them.最重要的是,如果需要,使用默认值并不会消除仍然具有多个重载的能力。 Typical examples where you want overloads with default values might be something like...您希望使用默认值进行重载的典型示例可能类似于...

ReadFrom(string filename, ReaderOptions options = null)
ReadFrom(Stream stream, ReaderOptions options = null)
ReadFrom(byte[] rawData, ReaderOptions options = null)

In these cases, I would argue the XML comments should actually be different.在这些情况下,我认为 XML 注释实际上应该有所不同。

A half-solution is the <overloads></overloads> tag.半解决方案是<overloads></overloads>标签。 While it doesn't solve the issue with <summary/> , it does provide documentation that shows up anywhere all the overloads are listed as a group, including both IntelliSense and SandCastle.虽然它没有解决<summary/>的问题,但它确实提供了显示所有重载作为一个组列出的任何地方的文档,包括 IntelliSense 和 SandCastle。

这是你想要的吗?

/// <seealso cref="SftpConnection(string,string,string,int)"</seealso>

InheritDoc works perfectly for overloads (at least in VS 2019). InheritDoc非常适合重载(至少在 VS 2019 中)。 You can override any part of it too.您也可以覆盖它的任何部分。 Official documentation says:官方文档说:

Inherit XML comments from base classes, interfaces, and similar methods .从基类、接口和类似方法继承 XML 注释。

/// <summary>
/// Method does something
/// </summary>
/// <param name="someString">Some string</param>
public void SomeMethod(string someString)
{
}

/// <param name="someInt">Some int</param>
/// <inheritdoc cref="SomeMethod(string)"/>
public void SomeMethod(string someString, int someInt)
{
}

/// <summary>Override the summary part</summary>
/// <param name="someString">Description for someString overridden</param>
/// <param name="anotherInt">Another int</param>
/// <inheritdoc cref="SomeMethod(string, int)"/>
public void SomeMethod(string someString, int someInt, int anotherInt)
{
}

I came from google and I'd like to share my solution based on the discussion above.我来自谷歌,我想根据上面的讨论分享我的解决方案。

Let's assume that you have two methods, one of them is an overload:假设您有两种方法,其中一种是重载:

public void MethodA(string paramA);
public void MethodA(string paramA, string paramB);

in order to map them to a XML file documentation you'd need to use the following commments:为了将它们映射到 XML 文件文档,您需要使用以下注释:

/// <include file='Docs/MyXMLFile.xml' path='docs/members/MethodA/*'/>
public void MethodA(string paramA);

/// <include file='Docs/MyXMLFile.xml' path='docs/members/MethodA/*'/>
public void MethodA(string paramA, string paramB);

And inside of your XML file, you need to use the <overloads> tag as informed by @Kache, the only thing which is important to note is the hierarchical structure which needs to be respected, so the final solution would be like this:在你的 XML 文件中,你需要使用 @Kache 告知的<overloads>标签,唯一需要注意的是需要尊重的层次结构,所以最终的解决方案是这样的:

in the MyXMLFile.xml在 MyXMLFile.xml 中

<overloads>
<MethodA>
      <summary>
       My MethodA...  
      </summary>
      <param name="paramA">
        My ParamA....
      </param>
</MethodA>
<MethodA>
      <summary>
       My MethodA...  
      </summary>
      <param name="paramA">
        My ParamA....
      </param>
      <param name="paramB">
        My ParamB....
      </param>
</MethodA>
</overloads>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM