怎么动态执行代码?

aa6103848252 2010-11-14 08:49:15
我找到个生成出来再运行的代码,但不是我想要的,我想只执行代码,不生成,下面是网站找的

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
...全文
153 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
libaolong1978 2010-11-15
  • 打赏
  • 举报
回复
帮顶一下吧,郁闷了好几天了!另外请教楼主是否知道如何在vb.net网站开发中,给panel控件添加mouseup事件?感谢!
yangzn76 2010-11-15
  • 打赏
  • 举报
回复
学习了,学习了
wsk200209 2010-11-15
  • 打赏
  • 举报
回复
学习 学习!
兔子-顾问 2010-11-15
  • 打赏
  • 举报
回复
就相当于你在项目上右击鼠标,选择添加引用,找到那个dll的效果。
你的类型来自于这个dll。所以要加载这个dll。
aa6103848252 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]

抱歉,是我没仔细看帖子。修改一下就可以了。
VB.NET code

Public Sub TestDynamicInvoke(ByVal code As String)
Dim Comp As New VBCodeProvider
Dim Parms As New System.CodeDom.Compiler.CompilerParameters ……
[/Quote]

太感谢了,就是我要的效果了,但是有点不明白
Parms.ReferencedAssemblies.Add("System.Windows.Forms.Dll")
这句干嘛的,还有我在System.Windows.Forms命名空间里怎么没找到Dll类?
兔子-顾问 2010-11-14
  • 打赏
  • 举报
回复
抱歉,是我没仔细看帖子。修改一下就可以了。

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


关键是要设置GenerateExecutable = False
启动程序需要动态加载。
aa6103848252 2010-11-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]

这个代码已经是你要的效果了。你可以这样测试。创建一个test.vb文件。保存如下文本
VB.NET code

Imports System.Windows.Forms
Module TestAssembly
Sub Main()
MessageBox.Show("Dynamically Created Code!")
End Sub
End Modu……
[/Quote]

在程序目录下生成了一个Output.exe啊,不是我要的效果...
vip__888 2010-11-14
  • 打赏
  • 举报
回复
饺子V5
兔子-顾问 2010-11-14
  • 打赏
  • 举报
回复
这个代码已经是你要的效果了。你可以这样测试。创建一个test.vb文件。保存如下文本

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


调用:
TestDynamicInvoke(File.ReadAllText("test.vb"))

已经是动态执行了。没生成dll的。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧