4

Does anyone know of any way to dynamically get the value of a parameter by name? I'm trying to create a function that will dynamically pass on its parameters. I'm using Reflection to get the name of the parameter but can't seem to figure out how to get the value that was passed to the function.

Example:

Imports System.Reflection

Console.WriteLine(Test("Xyzzy"))
' Should print: Xyzzy

Function Test(ByVal x as String)
  Return GetValueByName(MethodBase.GetCurrentMethod.GetParameters(0).Name))
End Function
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
user3183188
  • 41
  • 1
  • 3
  • 2
    Could you give a better example and maybe the goal behind it? In this case I would say: return the value of x – Patrick Hofman Jan 18 '14 at 22:12
  • 2
    It is currently not possible in .NET. See [this question](http://stackoverflow.com/questions/1867482/c-sharp-getting-value-of-parms-using-reflection) – Konrad Kokosa Jan 18 '14 at 22:14
  • If this code is going *inside* the same method that you wish to examine, then surely it doesn't need to be dynamic? As the author of the method you already know all the parameters. – nmclean Jan 18 '14 at 22:25
  • I'm going to be creating many functions that will take their parameters and call a routine in another class. I was just looking for a way to simplify those functions by not having to write the parameter name for every parameter on every function. Also would help if the parameters change, etc. – user3183188 Jan 18 '14 at 22:26
  • 1
    If the parameters are based on parameters in an external routine, then aren't you already hard-coding information that may change by defining the functions yourself? In a case like this it would probably be better to generate the source code itself, via [T4 template](http://msdn.microsoft.com/en-us/library/dd820620.aspx) for example. – nmclean Jan 18 '14 at 22:43
  • Actually, T4 templates might be useful. I'll look into them, thanks! – user3183188 Jan 18 '14 at 22:49
  • possible duplicate of [Can I get parameter names/values procedurally from the currently executing function?](http://stackoverflow.com/questions/2405230/can-i-get-parameter-names-values-procedurally-from-the-currently-executing-funct) – BlueMonkMN Feb 20 '14 at 16:05
  • here is a simular question for C#: [link][1] [1]: http://stackoverflow.com/questions/4939508/get-value-of-c-sharp-dynamic-property-via-string – Frank_Vr Mar 27 '14 at 13:14

2 Answers2

0

The following method will return the property (given the property name) value:

Public Function GetPropertyValue(propertyName As String) As Object

        Dim pi As System.Reflection.PropertyInfo = Me.GetType().GetProperty(propertyName)

        If (Not pi Is Nothing) And pi.CanRead Then
            Return pi.GetValue(Me, Nothing)
        End If

        Dim a As Type() = Nothing
        Dim mi As System.Reflection.MethodInfo = Me.GetType().GetMethod("Get" + propertyName, a)

        If Not mi Is Nothing Then
            Return mi.Invoke(Me, Nothing)
        End If

        Return Nothing
    End Function

Hope it helps

rubenerc
  • 59
  • 4
-1

If you want to print 'Xyzzy' then


Console.WriteLine(Test("Xyzzy"))
' Should print: Xyzzy
Function Test(ByVal x as String)
  Return x                 
End Function

BAASKA
  • 1
  • 3