简体   繁体   中英

custom Attributes for method arguments?

Is it possible to add a custom attribute to parameters of method.

For instance I have method as follows

''' <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

along with the "param name" and it description, Am i able to add any Tagged value to the parameter?

Frist we have to define calss, whose Attribute-Targe as Parameter as follows

<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

And then we can add the this attribute the any prameter as follows

''' <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

Thank you so much for all other reply and whoever put effort for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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