16,554
社区成员
发帖
与我相关
我的任务
分享
Dim Comp As New VBCodeProvider
Dim Parms As New System.CodeDom.Compiler.CompilerParameters With {.GenerateExecutable = True, .TreatWarningsAsErrors = False, .OutputAssembly = "Output"}
Parms.ReferencedAssemblies.Add("System.Windows.Forms.Dll")
Dim SampleCode As New System.Text.StringBuilder
SampleCode.Append("Imports System.Windows.Forms" & vbCrLf)
SampleCode.Append("Module TestAssembly" & vbCrLf)
SampleCode.Append("Sub Main()" & vbCrLf)
SampleCode.Append("MessageBox.Show(" + Chr(34) & _
"Dynamically Created Code!" + _
Chr(34) + ")" & vbCrLf)
SampleCode.Append("End Sub" & vbCrLf)
SampleCode.Append("End Module" & vbCrLf)
Dim Executable As System.CodeDom.Compiler.CompilerResults = Comp.CompileAssemblyFromSource(Parms, SampleCode.ToString)
If Executable.Errors.HasErrors Then
For Each Item As System.CodeDom.Compiler.CompilerError In Executable.Errors
MessageBox.Show(Item.ErrorText)
Next
Else
Process.Start("Output")
End If
Public Sub TestDynamicInvoke(ByVal code As String)
Dim Comp As New VBCodeProvider
Dim Parms As New System.CodeDom.Compiler.CompilerParameters With {.GenerateExecutable = False, .TreatWarningsAsErrors = False}
Parms.ReferencedAssemblies.Add("System.Windows.Forms.Dll")
Dim Executable As System.CodeDom.Compiler.CompilerResults = Comp.CompileAssemblyFromSource(Parms, code)
If Executable.Errors.HasErrors Then
For Each Item As System.CodeDom.Compiler.CompilerError In Executable.Errors
MessageBox.Show(Item.ErrorText)
Next
Else
Dim tempAssembly As Reflection.Assembly = Executable.CompiledAssembly
tempAssembly.GetType("TestAssembly").GetMethod("Main").Invoke(Nothing, Nothing)
End If
End Sub
Imports System.Windows.Forms
Module TestAssembly
Sub Main()
MessageBox.Show("Dynamically Created Code!")
End Sub
End Module
Public Sub TestDynamicInvoke(ByVal code As String)
Dim Comp As New VBCodeProvider
Dim Parms As New System.CodeDom.Compiler.CompilerParameters With {.GenerateExecutable = True, .TreatWarningsAsErrors = False, .OutputAssembly = "Output"}
Parms.ReferencedAssemblies.Add("System.Windows.Forms.Dll")
Dim Executable As System.CodeDom.Compiler.CompilerResults = Comp.CompileAssemblyFromSource(Parms, code)
If Executable.Errors.HasErrors Then
For Each Item As System.CodeDom.Compiler.CompilerError In Executable.Errors
MessageBox.Show(Item.ErrorText)
Next
Else
Process.Start("Output")
End If
End Sub