我有一个要编译成程序集的文本文件,使用 VBCodeProvider 类
该文件如下所示:
Imports System
Imports System.Data
Imports System.Windows.Forms
Class Script
Public Sub AfterClockIn(ByVal clockNo As Integer, ByRef comment As String)
If clockNo = 1234 Then
comment = "Not allowed"
MessageBox.Show(comment)
End If
End Sub
End Class
这是编译代码:
Private _scriptClass As Object
Private _scriptClassType As Type
Dim codeProvider As New Microsoft.VisualBasic.VBCodeProvider()
Dim optParams As New CompilerParameters
optParams.CompilerOptions = "/t:library"
optParams.GenerateInMemory = True
Dim results As CompilerResults = codeProvider.CompileAssemblyFromSource(optParams, code.ToString)
Dim assy As System.Reflection.Assembly = results.CompiledAssembly
_scriptClass = assy.CreateInstance("Script")
_scriptClassType = _scriptClass.GetType
我想要做的是修改方法内的注释字符串的值,以便在我从代码中调用它之后,我可以检查该值:
Dim comment As String = "Foo"
Dim method As MethodInfo = _scriptClassType.GetMethod("AfterClockIn")
method.Invoke(_scriptClass, New Object() {1234, comment})
Debug.WriteLine(comment)
但是评论总是"Foo"
(消息框显示"Not Allowed"
),所以看起来 ByRef 修饰符不起作用
如果我在我的代码中使用相同的方法comment
被正确修改。