It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Reflection on local variables

Moderator: dafunkymunky

Reflection on local variables

Postby cwalnum on Fri Feb 13, 2009 2:53 pm

Hello, all.

Is it possible to get the name of a local variable from a reference to the variable? For example, I can get the names and values of a calling function's parameters like this:

Code: Select all
Dim frame As New StackFrame(1)
Dim pInfos() As ParameterInfo = frame.GetMethod().GetParameters()

Is there some way to get the same information for a calling function's local variables? This is kind of what I have in mind:

Code: Select all
    Sub SomeSub()
        Dim count As Integer = 10
        Dim average As Single = 45.67
        LogLocalVariables(count, average)
    End Sub
    Sub LogLocalVariables(ByVal var1 As Object, ByVal var2 As Object)
        ' I can get each variable's type like this:
        Dim mBody As MethodBody = GetType(Form1).GetMethod _
            ("SomeSub").GetMethodBody()
        Dim varList As ObjectModel.ReadOnlyCollection(Of LocalVariableInfo) = _
            mBody.LocalVariables

        ' But what I really want is info like this:
        '     count = 10
        '     average = 45.67
    End Sub

Thanks.

--Clay
cwalnum
 
Posts: 1
Joined: Fri Feb 13, 2009 2:49 pm

Who is online

Users browsing this forum: No registered users and 2 guests