Public Class DynamicCompile
Public Sub CompileVB(ByVal Code As String)
Dim myProvider As New Microsoft.VisualBasic.VBCodeProvider
Dim Compailer As ICodeCompiler = myProvider.CreateCompiler
Dim CompailerParamentes As New CompilerParameters
Dim myAssembly As [Assembly]
Dim mySub As System.Reflection.MethodInfo
Dim MyClass1 As System.Type
With CompailerParamentes
.ReferencedAssemblies.Add("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll")
.ReferencedAssemblies.Add("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll")
'.OutputAssembly = "temp.dll"
.GenerateInMemory = True
End With
mySub = MyClass1.GetMethod("SayHello")
mySub.Invoke(MyClass1, Nothing)
End Sub
End Class
然后在窗体上
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myDC As New DynamicCompile
myDC.CompileVB(TextBox1.Text)