2011-12-09 74 views
0

下面是一些代码在运行时执行的代码:执行运行时代码参数

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 =:

这工作完全。我想知道如何将参数传递给函数。即

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

我很确保它是在下面几行:

Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage") 
    Info.Invoke(instance, Nothing) 

但我不能得到它的工作

回答

0

你。需要传递字符串值。

Info.Invoke(instance, New Object(){"Test"}) 

编辑:两种说法

Info.Invoke(instance, New Object(){"First","Second"}) 
+0

子ShowMessage(BYVAL X作为字符串) MessageBox.Show(X) 结束小组 –

+0

@SimonCanning - 对不起!你想说点什么吗?看看Invoke()方法的第二个参数。这样你可以传递一个参数给ShowMessage。 – adatapost

+0

太棒了。谢谢。 –