The following is some code to execute code at runtime:
Dim SourceCode As String = txtCode.Text
Dim Dlls() As String = {"System.dll", "System.Core.dll", "System.Data.dll", "System.Windows.Forms.dll"} 'Any referenced dll's
Dim Compiler As New VbCompiler(SourceCode, Dlls)
Dim CodeAssembly As Assembly = Compiler.Compile
If Compiler.Successful Then
Dim instance As Object = CodeAssembly.CreateInstance("TestCode.Class1")
Dim CodeType As Type = instance.GetType
Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage")
Info.Invoke(instance, Nothing)
Else
For Each i As CompilerError In Compiler.Errors
MsgBox(i.ErrorText)
Next
End If
txtCode.text =:
Imports System.Windows.Forms
Namespace TestCode
Class Class1
Sub ShowMessage()
MessageBox.Show("Test")
End Sub
End Class
End Namespace
This works perfectly. I am wanting to know how to pass arguments to a function. ie
txtCode.text = :
Imports System.Windows.Forms
Namespace TestCode
Class Class1
Sub ShowMessage(ByVal x As String)
MessageBox.Show(x)
End Sub
End Class
End Namespace
I would like to run the ‘ShowMessage” function with a string as the parameter, (“Test”) for an example.
I am pretty sure it is at the following lines:
Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage")
Info.Invoke(instance, Nothing)
But I cannot get it working.
You need to pass string value.
EDIT: Two arguments