自定义方法参数的属性?

时间:2015-03-24 08:45:44

标签: .net vb.net

是否可以为方法参数添加自定义属性。

例如我有如下方法

''' <summary>
'''  Get addition of given two number
''' </summary>
''' <param name="firstNumber">First number which participate in addition</param>
''' <param name="secondNumber">Second number which participate in addition</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Add(firstNumber As Integer, secondNumber As Integer) As Double
    Return firstNumber + secondNumber
End Function

以及&#34; param名称&#34;它的描述,我可以在参数中添加任何Tagged值吗?

1 个答案:

答案 0 :(得分:0)

我们必须定义calss,其Attribute-Targe为参数如下

<AttributeUsage(System.AttributeTargets.Parameter)>
Public Class SomeParameterAttribute
    Inherits Attribute

    Private m_Something As String

    Public Sub New(something As String)
        m_Something = something
    End Sub

    Public Property Something As String
        Get
            Return m_Something
        End Get
        Set(value As String)
            m_Something = value
        End Set
    End Property

End Class

然后我们可以将this属性添加到任何参数中,如下所示

''' <summary>
'''  Get addition of given two values
''' </summary>
''' <param name="firstNumber">First number which participate in addition</param>
''' <param name="secondNumber">Second number which participate in addition</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Add(<SomeParameterAttribute("Something about first number")> firstNumber As Integer, _
                    <SomeParameterAttribute("Something about second number")> secondNumber As Integer) As Double
    Return firstNumber + secondNumber
End Function

非常感谢所有其他回复以及为此付出努力的人。