c# – DRY XML注释

前端之家收集整理的这篇文章主要介绍了c# – DRY XML注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当提供相同方法的多个重载时,我经常不得不重复该方法的描述,这违反了DRY并增加了维护成本:
  1. /// <summary>
  2. /// Frobnicates all foos read from the given reader. Frobnication is a
  3. /// process where ...[lots of text]...
  4. /// </summary>
  5. /// <param name="hasBar">[Description of hasBar]</param>
  6. void FrobnicateFoo(TextReader reader,bool hasBar)
  7. {
  8. ...
  9. }
  10.  
  11. /// <summary>
  12. /// Frobnicates all foos read from the given file. Frobnication is a
  13. /// process where ...[same lots of text]...
  14. /// </summary>
  15. /// <param name="hasBar">[Same description of hasBar]</param>
  16. void FrobnicateFoo(String path,bool hasBar)
  17. {
  18. ...
  19. }

如果重复具有相同目的的多个参数,则该问题变得更糟(作为示例给出“hasBar”).

我找到的一个“解决方法”是“引用”其他文档:

  1. /// <summary>
  2. /// Frobnicates all foos read from the given reader. Frobnication is a
  3. /// process where ...[lots of text]...
  4. /// </summary>
  5. /// <param name="hasBar">[Description of hasBar]</param>
  6. void FrobnicateFoo(TextReader reader,bool hasBar)
  7. {
  8. ...
  9. }
  10.  
  11. /// <summary>
  12. /// Convenience method which opens the file with a UTF-8 encoding and then
  13. /// frobnicates all foos,see FrobnicateFoo(TextReader).
  14. /// </summary>
  15. void FrobnicateFoo(String path,bool hasBar)
  16. {
  17. ...
  18. }

显然,这对于图书馆的用户来说不太方便.

是否有一些内置机制(或智能策略)可以用来避免重复并让我的方法用户轻松生活?我主要关注IntelliSense,而不是生成的HTML文档.

解决方法

实际上有一个使用XML标签解决方案.实际上,您在XML文件中构建文档,然后将方法链接到此XML文件.这是我编写的一个小例子.

这里的解决方案是在VB.NET中,但我想将它转换为C#并不是很困难…

首先,您需要一个标准的库定义:

  1. ''' <summary>
  2. ''' This is my First class
  3. ''' </summary>
  4. ''' <remarks></remarks>
  5. Public Class FirstClass
  6. ''' <summary>
  7. ''' This is my first method
  8. ''' </summary>
  9. ''' <param name="A">A</param>
  10. ''' <returns>True</returns>
  11. ''' <remarks></remarks>
  12. Public Function FirstMethod(A As Integer) As Boolean
  13. Return True
  14. End Function
  15.  
  16. ''' <include file="DocFile.xml" path="Doc/member[@name='SecondMethod']/*" />
  17. Public Function SecondMethod(A As Integer) As String
  18. Return "Hello"
  19. End Function
  20.  
  21. ''' <include file="DocFile.xml" path="Doc/member[@name='SecondMethod']/*" />
  22. Public Function SecondMethod(A As Integer,B As String) As String
  23. Return "Hello"
  24. End Function
  25.  
  26. ''' <include file="DocFile.xml" path="Doc/member[@name='SecondMethod']/*" />
  27. Public Function SecondMethod(A As Integer,B As String,C As Boolean) As String
  28. Return "Hello"
  29. End Function
  30.  
  31. End Class

因此,课程和第一种方法的文档是“标准的”.对于SecondMethod,我提供了一个XML链接.

接下来你需要创建一个XML文件,这里称为DocFile.XML,你将在其中放置方法的文档:

  1. <Doc>
  2. <member name="SecondMethod">
  3. <summary>
  4. This is my second method
  5. </summary>
  6. <param name="A">a</param>
  7. <param name="B">b</param>
  8. <param name="C">c</param>
  9. <returns>A string containing "Hello"</returns>
  10. <remarks></remarks>
  11. </member>
  12. </Doc>

当你一起编译并创建文档(这里我使用SandCastle)时,它产生以下内容

对于每种方法

TLDR

>可以在XML文件中创建一次文档,并将方法链接到本文档.>您可以将许多方法链接到一个定义>案件很敏感> Visual Studio(这里我使用的是VS 2010 Express)在这个上并没有什么帮助,它不知道你在做什么.编译时,您将无法在项目的intellisense中看到它.如果您创建另一个解决方案并引用您的库,那么您将看到它.

猜你在找的C#相关文章