473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic Parameters With Reflection

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
1 2230
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
8
by: Eyeawanda Pondicherry | last post by:
I have put some code together that creates an enum dynamically from some database values. The enum can be read perfectly by an application that references the dynamically generated dll. If I...
5
by: Brad | last post by:
All samples related to this see to come short of being 'truly' dynamic. For instance, after creating all the code to load/save a properties value, you turn around and save it to one you KNOW...
3
by: Jakob Lithner | last post by:
I have searched the news groups on similar subjects, but haven't found anything adequate for my need .... To save much duplication of code I would like to create a baseclass that takes a...
1
by: Derek Hart | last post by:
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...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
13
by: DaTurk | last post by:
Hi, This is a question brought about by a solution I came up with to another question I had, which was "Dynamic object creation". So, I'm curious if you can dynamically cast an object. If you...
2
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I am facing a problem in dynamic code generation at run time, the problem occurs when the dynmaic code use global data exist within the original application. Lets say that my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.