Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本文件动态读取/编译vb.net代码_Vb.net - Fatal编程技术网

从文本文件动态读取/编译vb.net代码

从文本文件动态读取/编译vb.net代码,vb.net,Vb.net,我试图从文本文件中读取和编译代码。我已经完成了它的一些部分,但在尝试向表单添加控件时感到困难。请引导我。我附上代码 Public Class Form1 Sub Execute() ' Creates object of the compiler Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler

我试图从文本文件中读取和编译代码。我已经完成了它的一些部分,但在尝试向表单添加控件时感到困难。请引导我。我附上代码

Public Class Form1

    Sub Execute()

        ' Creates object of the compiler
        Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler

        'References/Parameters.
        Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()

        objCompilerParameters.ReferencedAssemblies.Add("System.dll")
        objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")


        'Compiles in memory.
        objCompilerParameters.GenerateInMemory = True

        'Runs the source code.
        'You can use resources, textbox's or even the settings, up to you! :D
        'Dim strCode As String = TextBox1.Text

        'Compiler Results
        'Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)

        Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, "H:\VB project\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\file.txt")

        'If an Error occurs
        If objCompileResults.Errors.HasErrors Then
            MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & objCompileResults.Errors(0).ErrorText)
            Exit Sub
        End If

        'Creates assembly
        Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly

        Dim objTheClass As Object = objAssembly.CreateInstance("MainClass")
        If objTheClass Is Nothing Then
            MsgBox("Can't load class...")
            Exit Sub
        End If

        'Trys to excute
        Try
            objTheClass.GetType.InvokeMember("ExecuteCode",
                System.Reflection.BindingFlags.InvokeMethod, Nothing, objTheClass, Nothing)
        Catch ex As Exception
            MsgBox("Error:" & ex.Message)
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Runs the source code from textbox1.
        Execute()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim p As New Form2
        p.Text = "hahahahah"
        p.Show()


    End Sub
End Class
“未定义类型Form2”

那是因为你还没有定义它。您可能正在动态加载和编译某些文件,其中包含定义该文件的代码。但是编译器在编译时无法知道这一点。作为一个静态类型系统,编译器在编译代码时需要了解您使用的所有类型


如果要从文件中动态加载类定义,则还需要动态调用这些类定义。这会涉及很多其他的事情。它不会很漂亮,而且肯定不会有编译时类型检查。(它将是非常“严格类型化”的,即不是创建
Form2
的实例,而是请求反射来创建
“Form2”
的实例,并希望达到最好的效果。)因此,您将需要对一些事情进行大量的错误处理,例如,不存在的类。

您有问题吗?有错误吗?什么不起作用?“引导我”不是很具体,我认为Visual Studio不支持这一点。当我尝试将表单“Dim p As New Form2 p.Show()”生成时,出现错误“Type Form2未定义”,但简单代码正在运行。我已经试过hello word。
H:…file.txt
需要包含有效的VB代码对不起,先生,实际上我是.net新手。我不明白你的意思,你是说用这种方式动态处理代码不好吗?@RanaTariq:在几乎99.9%的情况下,是的。需要考虑的因素很多,包括安全性和可能出现严重错误。除非您对.NET的内部工作原理非常了解,否则不应该采用这种方法。(我认为自己是一位经验丰富的专家,我个人会不惜一切代价避免这样做。)如果你是新来的.NET,为什么你要一开始就这么做?为什么不编写一个标准应用程序呢?你想用这个做什么?我需要做一个简单的物理方程编辑器和求解器。我正试图把新的方程式加入到系统中。当用户添加新的等式时,文本框(变量)的代码应自动写入,并与该特定等式相关的其他逻辑。@RanaTariq:正如您在前面的评论中所提到的,您在“hello world”中“得到了它”。因此,或许可以回到那次成功,看看你是否能在它的基础上再接再厉。创建类显然是行不通的(如果没有我答案中链接带来的一点开销的话),但是假设您能够完全成功地执行过程代码?如果是这样的话,那么输入文件可能只需要是没有类的过程代码?随着您越来越习惯于这一切是如何结合在一起的,您可以研究随着时间的推移添加这样的功能。