run function from string

G

Guest

Hi,

I've a question. I'm developing a windows application and for the
application i need to run functions and procedures that are stored in a
database.
Here is an example that i tried to get working.


How can i execute the function that is stored in the string expr ?

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim expr As String = "test2(10,25)"
MessageBox.Show(expr)
expr.run()
End Sub

Function test2(ByVal i As Integer, ByVal x As Integer) As Boolean
Dim y As Integer
y = i + x
Return y
End Function
 
A

Abdul Aleem

I assume you already have the 'test2' function in the same class from where
you want to invoke it.
You can invoke the function in the expr as follows

Dim expr As String = "test2"

Dim obj As Object = Activator.CreateInstance(Me.GetType(), New String() {})

Dim result As Object = Me.GetType.InvokeMember(expr,
Reflection.BindingFlags.Default Or Reflection.BindingFlags.InvokeMethod,
Nothing, Me, New Object() {10, 25})

MessageBox.Show(CType(result, Integer).ToString())

Note that I have passed your parameters (10, 25) as the final parameter to
the InvokeMember function which is a param array. Also correct your test2
function, I think it is required to return an integer whereas the return
type is boolean.
 
K

Kevin Spencer

You will have to write code that parses the text and calls the appropriate
function.

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Guest

Imports System.Text.RegularExpressions
Imports System.Reflection

Module Module1
Class Expression
Private Shared methodRegex As Regex = New
Regex("^(?<Method>\w[\w\d_]+)\s*(\((?<Parameters>.*)\))?$",
RegexOptions.Compiled Or RegexOptions.Singleline)
Public Shared Function Run(ByVal obj As Object, ByVal expr As
String) As Object
Dim m As Match = methodRegex.Match(expr)
Dim method As String = m.Groups("Method").Value
Dim parameters As String = m.Groups("Parameters").Value
Dim params As Object() = Nothing

Dim binding As BindingFlags = BindingFlags.InvokeMethod Or
BindingFlags.Instance Or BindingFlags.Static Or BindingFlags.Public Or
BindingFlags.NonPublic

Dim mi As MethodInfo = obj.GetType().GetMethod(method, binding)

If mi Is Nothing Then
Return Nothing
End If

If Not String.IsNullOrEmpty(parameters) Then
Dim stringParams As String() = parameters.Split(New String()
{","}, StringSplitOptions.RemoveEmptyEntries)
ReDim params(stringParams.Length - 1)

Dim parameterInfo As ParameterInfo() = mi.GetParameters()

If params.Length <> parameterInfo.Length Then
Return Nothing
End If

For Each pi As ParameterInfo In parameterInfo
params(pi.Position) =
Convert.ChangeType(stringParams(pi.Position), pi.ParameterType)
Next
End If

Return mi.Invoke(obj, binding, Nothing, params,
Threading.Thread.CurrentThread.CurrentCulture)
End Function
End Class

Sub Main()
Dim expr As String = "test2(10,25)"
Expression.Run(New TestClass, expr)
End Sub

Class TestClass
Public Function test2(ByVal i As Integer, ByVal x As Integer) As
Integer
Dim y As Integer
y = i + x
Return y
End Function
End Class
End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top