Vb.net 执行运行时代码参数
以下是一些在运行时执行代码的代码:Vb.net 执行运行时代码参数,vb.net,Vb.net,以下是一些在运行时执行代码的代码: 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 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
这很好用。我想知道如何将参数传递给函数。即
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
我想使用字符串作为参数运行'ShowMessage函数,例如Test
我很确定这是在以下几行:
Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage")
Info.Invoke(instance, Nothing)
但我无法让它工作。您需要传递字符串值
Info.Invoke(instance, New Object(){"Test"})
编辑:两个参数
Info.Invoke(instance, New Object(){"First","Second"})
子ShowMessageByVal x作为字符串MessageBox.Showx结束Sub@SimonCanning-对不起!你想说点什么吗?看看Invoke方法的第二个参数。这样就可以将参数传递给ShowMessage。