locked
Iterating through the parameters of the current method [MSIL] RRS feed

  • Question

  • 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 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.

    Sunday, July 15, 2012 10:37 PM

Answers

  • Hi Xero,

    Welcome to the MSDN Forum.

    How about make a new class named ParameterAndValue, It has two properties, one is string and the other is object:

    Public Class ParameterAndValue
        Public Property ParameterType As String
        Public Property PValue As Object
        Public Sub New(_ParameterType As String, _PValue As Object)
            ParameterType = _ParameterType
            PValue = _PValue
        End Sub
    End Class

    So your code can be changed like this:

        Public Sub SampleHandler(ByVal Sender As Object, ByVal e As EventArgs)
            Dim argholder As New List(Of ParameterAndValue)
            argholder.Add(New ParameterAndValue(Sender.GetType().Name, Sender))
            argholder.Add(New ParameterAndValue(e.GetType().Name, Sender))
            
            Log(argholder)
            'Continue doing method things
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Wednesday, July 25, 2012 10:18 AM
    Monday, July 16, 2012 10:32 AM

All replies

  • Hi Xero,

    Welcome to the MSDN Forum.

    How about make a new class named ParameterAndValue, It has two properties, one is string and the other is object:

    Public Class ParameterAndValue
        Public Property ParameterType As String
        Public Property PValue As Object
        Public Sub New(_ParameterType As String, _PValue As Object)
            ParameterType = _ParameterType
            PValue = _PValue
        End Sub
    End Class

    So your code can be changed like this:

        Public Sub SampleHandler(ByVal Sender As Object, ByVal e As EventArgs)
            Dim argholder As New List(Of ParameterAndValue)
            argholder.Add(New ParameterAndValue(Sender.GetType().Name, Sender))
            argholder.Add(New ParameterAndValue(e.GetType().Name, Sender))
            
            Log(argholder)
            'Continue doing method things
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Wednesday, July 25, 2012 10:18 AM
    Monday, July 16, 2012 10:32 AM
  • Hi Xero,

    I suppose that you already look at "Reflection", but unfortunately it is not possible to get the values of the parameters using reflection. You will be able to get a lot of things but not the values. To acheive this, you would have to do yourself instrospection of the stack and this is clearly very hard. (For exemple, due to JIT compiler optimizations, the parameter may have a different type that the method signature suggests).

    I had the same desire than you have, I have done a lot of searches throught the internet. It seems that only ways to analyze the stack are :

    • Using the Profiling API
    • Using a debugger API (such as Visual Studio)
    • Using code injection (but I really didn't undestand how this can be done)

    To be clear simple reflective techniques cannot achieve what you desire with full generality, so the easiest will probably to use Mike's idea, even if it's not generic and you have to change code each time you copy/paste to another function.

    Jojo


    • Edited by JojoShow Monday, July 16, 2012 4:18 PM
    Monday, July 16, 2012 4:17 PM