【问题标题】:Adding annotations to interfaces向接口添加注解
【发布时间】:2013-07-03 21:14:48
【问题描述】:

我在 .NET 框架中看到过这样的案例,当您实现一个接口时,Visual Studio 会生成 cmets(和其他好东西,如区域)。

一个很好的例子是IDisposable。当你实现它时,Visual Studio 会生成以下代码块:

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

这是我可以在我自己的代码中做的事情吗?如果是这样,怎么做?我想在我的接口方法中添加一些 cmets,告诉实现者该方法的一般用途是什么。

【问题讨论】:

标签: vb.net interface


【解决方案1】:

我不确定这是否 100% 符合您的要求,但这里是。

在你的方法定义类型'''之上

编辑器将其扩展为一段 XML,用于在使用该方法时驱动智能感知。

Private Sub Import(ByVal ImportFileName As String, ByRef OutFileName As String)

变成

''' <summary>
''' Imports data from import file into Output File
''' </summary>
''' <param name="ImportFileName">the fully qualified path to the import file</param>
''' <param name="OutFileName">the fully qualified path for the output file</param>
''' <remarks>all the widgets need to be thoroughly castigated</remarks>
Private Sub Import(ByVal ImportFileName As String, ByRef OutFileName As String)

每当使用此 Import 方法时,所提供的提示都会显示在参数名称旁边。

【讨论】:

  • 这可能是我能得到的最接近的,但我真的在寻找一种方法来为我的接口提供内联代码文档。只有编写实现的程序员才能看到它们,而不是所有使用它的人。
  • 如果你曾经用VB写过一个实现IDisposable的类,你就会明白我的意思。添加接口后,您会立即获得一个带有 TODO cmets 的默认实现。这正是我想做的事情。
  • 我已经使用了 IDisposable 接口并且确切地知道您的意思。但是根据上面的链接问题,IDisposable 的可爱 TODO 内容被硬编码到 VS 编辑器中。你最好的当然是有sn-ps,
  • 如果完全不可能编写具有类似功能的 Visual Studio 插件,我会感到惊讶。当然,您需要在每台开发机器上都安装它,这不是我可以从我的用户那里得到的。
猜你喜欢
  • 1970-01-01
  • 2015-01-24
  • 2014-08-30
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多