1

In VB.Net Compact Framework 3.5 is it possible to get a list of the Parameters that are passed to a method?

For example,

Private Sub TestSub(Param1 as String, Param2 as Integer, Param3 as List(Of String)
    'Get List of Parameters 
End Sub

Is it possible to get the names of the parameters and what they are populated with at the point it says 'Get List of Parameters

Any help would be much appreciated.

Thanks

Ste Moore
  • 107
  • 1
  • 10
  • "Get a list of Parameters" In what sense? Intellisense? (Otherwise, it is already right there) – Ian Jan 29 '16 at 08:56
  • 1
    I guess he wants to get all the parameters in a subroutine or function. I myself want to know also. But I think it's impossible according here. https://stackoverflow.com/questions/7236746/get-the-name-of-the-object-passed-in-a-byref-parameter-vb-net – Aethan Jan 29 '16 at 08:58
  • Sorry for the confusion. I'm trying to add a simple error handler to all my functions and subs. I want to be able to add the same piece of code to all my methods so that it gives a list of the parameter names and the content of that parameter when it errors. I can go through my code and manually type this in for all my methods but it will be quite time consuming and I'm on a deadline. From the link Crush Sundae provided, it appears you need GetCurrentMethod which isn't available in Compact Framework, but if anyone knows of another way, I'd appreciate the help. – Ste Moore Jan 29 '16 at 10:08
  • I'd stop short of saying this is a duplicate but I think the information you're after is here: http://stackoverflow.com/questions/531695/how-to-print-the-current-stack-trace-in-net-without-any-exception – 5uperdan Jan 29 '16 at 10:30
  • Also see this: https://stackoverflow.com/q/3288597/737393 – CrazyTim Aug 27 '19 at 23:47

1 Answers1

2

Take a look at https://msdn.microsoft.com/pt-br/library/system.reflection.methodbase.getcurrentmethod(v=vs.110).aspx and https://msdn.microsoft.com/pt-br/library/system.reflection.methodbase.getparameters(v=vs.110).aspx for further information.

But I'm assuming that you're looking for something like this:

Private Function GetParameters(ByVal info As MethodBase) As String
    Dim lst = info.GetParameters()
    Dim strParameters As String = ""
    For Each item In lst
        If strParameters <> "" Then strParameters += ","
        strParameters += item.Name
    Next

    Return strParameters

End Function

And to invoke:

Private Sub TestSub(ByRef a As String, ByRef b as String)
    Dim strParameters As String = GetParameters(System.Reflection.MethodBase.GetCurrentMethod())
End Sub

The return should be "a,b".

Best regards.

Abner
  • 416
  • 5
  • 18