gpt4 book ai didi

vb.net - 在运行时编译和运行代码

转载 作者:行者123 更新时间:2023-12-04 19:37:53 32 4
gpt4 key购买 nike

我正在尝试在运行时编译和运行代码。我正在使用下面的代码来实现这一点。但是,当我尝试调用该方法时,只会打开一个“查找源”文件浏览器对话框,并且代码不会运行。任何人都可以在这里帮助我。

Dim VBP As New VBCodeProvider
Dim CVB As System.CodeDom.Compiler.ICodeCompiler

CVB = VBP.CreateCompiler
Dim PM As New System.CodeDom.Compiler.CompilerParameters

PM.GenerateInMemory = True
PM.GenerateExecutable = True
PM.OutputAssembly = "RunCode.dll"
PM.MainClass = "MainClass"
PM.IncludeDebugInformation = True

Dim ASM As System.Reflection.Assembly
For Each ASM In AppDomain.CurrentDomain.GetAssemblies
PM.ReferencedAssemblies.Add(ASM.Location)
Next
Dim CompileResults As System.CodeDom.Compiler.CompilerResults

CompileResults = CVB.CompileAssemblyFromSource(PM, sCode)

Dim CompileErrors As System.CodeDom.Compiler.CompilerError

For Each CompileErrors In CompileResults.Errors
RTMainScript.AppendText(vbCrLf & CompileErrors.ErrorNumber & ": " & CompileErrors.ErrorText & ", " & CompileErrors.Line)
Next

Dim objRun As New Object
Dim vArgs() As Object

objRun = CompileResults.CompiledAssembly.CreateInstance("RunCode.MainClass", False, BindingFlags.CreateInstance, Nothing, vArgs, Nothing, Nothing)
If Not objRun Is Nothing Then
Dim oMethodInfo As MethodInfo = objRun.GetType().GetMethod("Main")
Dim oRetObj As Object = oMethodInfo.Invoke(objRun, BindingFlags.Static Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic, Nothing, Nothing, Nothing) 'Find source dialog appears here
Else
MsgBox("Compile Error")
End If

最佳答案

您提供的代码不完整。您正在使用此方法编译代码:

CompileResults = CVB.CompileAssemblyFromSource(PM, sCode)

但是您实际上从未指定什么是sCode。如果您看到一个打开的文件浏览器对话框,那么我很确定您的 sCode 是它的原因。它必须在计算打开文件的变量值时在某处设置。

如果您尝试更改用于从文件编译的一段代码,那么将方法从 CompileAssemblyFromFile() 更改为 CompileAssemblyFromSource() 是不够的.您需要深入研究代码并更改所有相关方法。

关于vb.net - 在运行时编译和运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627368/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com