By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Dynamic Parameters With Reflection

P: n/a
I have the following code:

Me.GetType().InvokeMember(FunctionToRun, BindingFlags.InvokeMethod, Nothing, Me, params)

I can send in a string from a database into the FunctionToRun variable, and it will run the method in the current class, but I need to read parameters from a database into the params object, and this is where I am stuck. It seems that the params may need to be actual variables, but I am not sure. How can I take a list of params that are located in a database (basically, just a string) and use this in the params for the reflection call?

Derek
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
SR
Hi Derek

Refer to the code below which shows how to pass parameter
(s) and to trap return values etc. Hope this helps. In
case u still have a problem, do mail/post me

Watch for Line wraps...
------------------------
Public Sub CallMethodsThruReflection()

'No Parameters
fnExecuteMethods(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethods(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethods(Me, "fn3", l_objParams3)
'One String Parameter and One Integer Parameter
(byref param, modified inside the fn)
Dim l_objParams4(1) As Object
l_objParams4(0) = "Hi"
l_objParams4(1) = 20
print(l_objParams4(1))
fnExecuteMethods(Me, "fn4", l_objParams4)
print(l_objParams4(1))
'One String Parameter and One Integer Parameter
and One String as return Parameter
Dim l_objParams5(1) As Object
l_objParams5(0) = "Hi"
l_objParams5(1) = 20
Dim l_strRetVal As String = fnExecuteMethods
(Me, "fn5", l_objParams5).ToString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethods(ByVal p_objObject As
Object, ByVal p_objMethodName As String, Optional ByVal
p_objParams() As Object = Nothing) As Object

' Create a type object and store the type of the
object passed
Dim l_objType As Type = p_objObject.GetType
'Declare a MethodInfo object to store the Methods
of the class
Dim l_objMethodInfo As MethodInfo
'Declare a variable to loop through the Methods of
this class
Dim l_intMethodCtr As Integer

' Get the MethodInfo for the current class.
Binding Flags are specified to get the
' public and private Methods of this class. When
Public or Non-Public is specified
' in the BindingFlags, it is also necessary to
specify Static or Instance
l_objMethodInfo = l_objType.GetMethod
(p_objMethodName, BindingFlags.NonPublic Or
BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return l_objMethodInfo.Invoke(p_objObject,
p_objParams)

End Function

Public Sub fn1()
print("fn1 was called")
End Sub

Public Sub fn2(ByVal p_strData As String)
print("fn2 was called with parameter : " &
p_strData)
End Sub

Public Sub fn3(ByVal p_intData As Integer)
print("fn3 was called with parameter : " &
p_intData)
End Sub

Public Sub fn4(ByVal p_strData As String, ByRef
p_intData As Integer)
print("fn4 was called with parameters : " &
p_strData & " AND " & p_intData)
p_intData = 100
End Sub

Public Function fn5(ByVal p_strData As String, ByVal
p_intData As Integer) As String
print("fn5 was called with parameters : " &
p_strData & " AND " & p_intData & ". Returning a
concatenated value.")
Return p_strData & p_intData
End Function

Public Sub print(ByVal p_objObject As Object)
Console.WriteLine(p_objObject)
End Sub

hth

regards,

sr

-----Original Message-----
I have the following code:

Me.GetType().InvokeMember(FunctionToRun, BindingFlags.InvokeMethod, Nothing, Me, params)
I can send in a string from a database into the FunctionToRun variable, and it will run the method in the
current class, but I need to read parameters from a
database into the params object, and this is where I am
stuck. It seems that the params may need to be actual
variables, but I am not sure. How can I take a list of
params that are located in a database (basically, just a
string) and use this in the params for the reflection call?
Derek

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.