1

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.

Garandy
  • 203
  • 1
  • 10

2 Answers2

1

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
SSS
  • 4,807
  • 1
  • 23
  • 44
  • If I end up having to do this manually, then I'll go with this method, as it's the cleanest implementation I've found for it. I'm going to hold out that someone has a good answer. – Garandy Jul 16 '12 at 04:43
0

You could try System.Reflection.MethodBase.GetCurrentMethod and then use the returned methods GetParameters function.

Having said that, there is some confusion/disagreement as to whether or not this will actually work, but it is probably worth a try if for no other reason than to eliminate it as a possibility.

Community
  • 1
  • 1
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 1
    I like the train of thought and have already been down that alley: GetParameters returns the name, type, and default value of each parameter, but sadly, the name can't be used as a referenceable identifier, and it doesn't hold a value (a few hours of testing showed that). That thread does bring up the idea of interception, though, which could work... – Garandy Jul 16 '12 at 04:45