如何编译用户输入的代码? [英] How to compile code Entered by the user?

查看:82
本文介绍了如何编译用户输入的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个简单的 vb.net 程序,该程序运行用户输入的一段代码(也在vb.net中)。但是我需要我的程序来编译和运行它。

I need to make a simple vb.net program that runs a piece of code entered by the user (also in vb.net). But I need my program to compile and run it.

任何人都知道如何执行此操作吗?

Anyone have an idea on how to do this?

推荐答案

实际上,几年前,我实际上写了一篇博客文章(下面的链接)。下面的示例来自2010年,今天可能有更好的方法来解决此问题。可以在代码注释中找到更多的解释。

I actually wrote a blog post (link below) about this several years ago. The below example is from 2010, and there may be better methods to solve this problem today. More explanation can be found in the code comments.

本质上:


  • 阅读文件中的代码。

  • 创建VB.NET CodeProvider的实例

  • 创建编译器参数并将其传递给编译器

  • 编译代码

  • 检查错误

  • 创建包含代码的类的实例

  • 创建传递给我们的已编译代码的参数

  • 执行代码并查看结果

  • Read the code from the file.
  • Create an instance of a VB.NET CodeProvider
  • Create compiler parameters and pass them to the compiler
  • Compile the code
  • Check for errors
  • Create an instance of the class containing the code
  • Create arguments to pass to our compiled code
  • Execute the code and see results

下面是用于在文本文件中执行代码并在TextBox中显示结果的方法,但是可以很容易地将其用于解析Textbox中的代码。 (有关详细信息,请参见 vbCity Blog ):

An example of this being utilized to execute code in a text file and displaying a result in a TextBox is below, but could easily be used to parse code from a Textbox. (more info at vbCity Blog):

包括:

Imports System.IO   
Imports System.Reflection   
Imports System.CodeDom   
Imports System.CodeDom.Compiler   
Imports Microsoft.VisualBasic

代码:

' Read code from file
Dim input = My.Computer.FileSystem.ReadAllText("Code.txt")

' Create "code" literal to pass to the compiler.
'
' Notice the <% = input % > where the code read from the text file (Code.txt) 
' is inserted into the code fragment.
Dim code = <code>
               Imports System
               Imports System.Windows.Forms

               Public Class TempClass
                   Public Sub UpdateText(ByVal txtOutput As TextBox)
                       <%= input %>
                   End Sub
               End Class
           </code>

' Create the VB.NET Code Provider.
Dim vbProv = New VBCodeProvider()
' Create parameters to pass to the compiler.
Dim vbParams = New CompilerParameters()
' Add referenced assemblies.
vbParams.ReferencedAssemblies.Add("mscorlib.dll")
vbParams.ReferencedAssemblies.Add("System.dll")
vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
vbParams.GenerateExecutable = False
' Ensure we generate an assembly in memory and not as a physical file.
vbParams.GenerateInMemory = True

' Compile the code and get the compiler results (contains errors, etc.)
Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)

' Check for compile errors
If compResults.Errors.Count > 0 Then

    ' Show each error.
    For Each er In compResults.Errors
        MessageBox.Show(er.ToString())
    Next

Else

    ' Create instance of the temporary compiled class.
    Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
    ' An array of object that represent the arguments to be passed to our method (UpdateText).
    Dim args() As Object = {Me.txtOutput}
    ' Execute the method by passing the method name and arguments.
    Dim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)

End If

这篇关于如何编译用户输入的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆