【问题标题】:How to compile code Entered by the user?如何编译用户输入的代码?
【发布时间】:2014-04-11 10:18:52
【问题描述】:

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

有人知道怎么做吗?

【问题讨论】:

    标签: vb.net compiler-construction interface


    【解决方案1】:

    我实际上在几年前写了一篇关于这个的博客文章(下面的链接)。下面的例子是 2010 年的,今天可能有更好的方法来解决这个问题。更多解释见代码cmets。

    基本上:

    • 从文件中读取代码。
    • 创建 VB.NET CodeProvider 的实例
    • 创建编译器参数并将它们传递给编译器
    • 编译代码
    • 检查错误
    • 创建包含代码的类的实例
    • 创建参数以传递给我们的编译代码
    • 执行代码并查看结果

    下面是一个用于执行文本文件中的代码并在 TextBox 中显示结果的示例,但可以很容易地用于解析来自 Textbox 的代码。 (更多信息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
    

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 2013-07-30
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 2014-01-10
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多