【发布时间】:2017-01-18 09:43:56
【问题描述】:
在提供同一个方法的多个重载时,我经常不得不重复方法的描述,这违反了DRY,增加了维护成本:
/// <summary>
/// Frobnicates all foos read from the given reader. Frobnication is a
/// process where ...[lots of text]...
/// </summary>
/// <param name="hasBar">[Description of hasBar]</param>
void FrobnicateFoo(TextReader reader, bool hasBar)
{
...
}
/// <summary>
/// Frobnicates all foos read from the given file. Frobnication is a
/// process where ...[same lots of text]...
/// </summary>
/// <param name="hasBar">[Same description of hasBar]</param>
void FrobnicateFoo(String path, bool hasBar)
{
...
}
如果重复多个具有相同目的的参数,这个问题会变得更糟(以“hasBar”为例)。
我发现的一个“解决方法”是“参考”其他文档:
/// <summary>
/// Frobnicates all foos read from the given reader. Frobnication is a
/// process where ...[lots of text]...
/// </summary>
/// <param name="hasBar">[Description of hasBar]</param>
void FrobnicateFoo(TextReader reader, bool hasBar)
{
...
}
/// <summary>
/// Convenience method which opens the file with a UTF-8 encoding and then
/// frobnicates all foos, see FrobnicateFoo(TextReader).
/// </summary>
void FrobnicateFoo(String path, bool hasBar)
{
...
}
显然,这对图书馆的用户来说不太方便。
是否有一些内置机制(或智能策略)可以用来避免重复和让我的方法的用户更轻松?我主要关心 IntelliSense,而不是生成的 HTML 文档。
【问题讨论】:
-
虽然我明白你为什么将它们添加为标签,但这不是 C# 或 VB 特定的问题......也许是 .NET 代替?
-
@DanielShillcock:如果有的话,我完全可以使用 C# 或 VB 解决方案。 :-) 有些 .NET 语言根本不支持 XML cmets(例如,Boo)。
-
我相信您的问题没有答案。在撰写文档时,您会经常重复自己:(
-
我能想到的对您所做的唯一改进是使用
<see cref=""/>标签引用另一种方法。对于许多文档工具,这将生成指向其他方法的链接。 -
没有解决方案,但长期以来一直建议 Roslyn:github.com/dotnet/roslyn/issues/67
标签: c# vb.net documentation xml-documentation