Getting refference to a method parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikolay Petrov

    #1

    Getting refference to a method parameters

    I am implementing a logging facility for my app, and I am looking for a
    way to iterate through the parametersm passed to a method.
    Something like:

    Sub Test(byval ABCD as string, byval EFGH as integer)
    Try
    ' Some Code
    Catch ex as Exception
    'Code to find what types are the two parameters
    ' and do some stuff with them like logging or something
    End Try
    End Sub

    Of course I know that this method has two patamaters and what is their
    type, but I want to write independent code, which will work for every
    method. My best guess is for using reflection, but I don't know how to
    reference the method's parameters.

    Any ideas?

    tnx

  • Chris Dunaway

    #2
    Re: Getting refference to a method parameters

    You can try this code:

    Private Sub TestMe(MyString As String, MyInteger As Integer)
    Dim sf As New StackFrame(True )
    Dim mb As MethodBase = sf.GetMethod()

    Dim pi As ParameterInfo() = mb.GetParameter s()

    For Each p As ParameterInfo In pi
    Console.WriteLi ne(p.Name & ": " & p.ParameterType .ToString)
    Next
    End Sub

    Comment

    • Nikolay Petrov

      #3
      Re: Getting refference to a method parameters

      What about the values?

      Comment

      • Chris Dunaway

        #4
        Re: Getting refference to a method parameters

        Your original post did not mention getting the values.

        I didn't look very deeply at the ParameterInfo class and assumed that
        it would provide a method for getting the values but, alas, it does
        not!

        I'm not sure how you would get the values in this context.

        Comment

        • Nikolay Petrov

          #5
          Re: Getting refference to a method parameters

          Thanks Chris

          Chris Dunaway wrote:[color=blue]
          > Your original post did not mention getting the values.
          >
          > I didn't look very deeply at the ParameterInfo class and assumed that
          > it would provide a method for getting the values but, alas, it does
          > not!
          >
          > I'm not sure how you would get the values in this context.[/color]

          Comment

          Working...