Search code examples
.netvb.netcil

Iterating through the parameters of the current method


Hey Guys I need to be able to generically iterate through all of the arguments of the current function/sub (for logging), such as the following:

Public sub SampleHandler(ByVal Sender as Object, ByVal e as EventArgs)
    Dim argholder as List(of Object)
    For each arg in getcurrentargs() '<-------- That thing
        argholder.add(arg)
    Next
    Log(argholder)
    'Continue doing method things
End Sub

Any thoughts? I've been trying to extract them through Emit (which I don't entirely grasp) and StackTrace, but so far to no avail. I've been told by multiple sources that MSIL would be able to accomplish this, but no one I've talked to can give me a working implementation or clear explanation of HOW.

Additional info:

I need to be able to iterate through all the arguments to the function and add their values into a list. Whether this be through some method that extracts the names and then can utilize them to get the values, or just some sort of generic identifier that can reference them, either way is fine, but it has to be programmatic and a static code block.

The intent is that in any non-sensitive function (of which the code I'm working with for this project has a few hundred), I want to be able drop in an inline reference to this code block (figured out the method for that) to capture the name of the function (done), it's module/class (done), when it ran (done), the result (if a function; done), what the current err_level is (done) and what values are being passed in as parameters (on the logic that I know the module/class, and the function name, I can just match the values if necessary), so that I can turn around and log them. The 'sensitive' functions, I'm having to handle differently, so that I'm not logging out p/w and u/n or anything like that; but those are taken care of.


Solution

  • One possibility is to change your Log() routine to use a paramarray...

    Sub Log(ParamArray o() As Object)
      For i As Integer = 0 To o.GetUpperBound(0)
        '...do stuff....
      Next
    End Sub
    

    Then add every parameter when calling ...

    Public sub SampleHandler(ByVal Sender as Object, ByVal e as EventArgs)
        Call Log(Sender, e)
        'Continue doing method things
    End Sub