如何以编程方式编译vb.net代码并参考项目代码?

时间:2019-06-07 22:34:30

标签: vb.net

我正在创建一个游戏,该游戏从文本文件中获取用户编写的代码并进行编译,然后编译并运行该代码,但是我希望该代码能够访问一些公共变量和类,在我的项目中。

我尝试添加对XML文件的引用,我的程序称为Puppeteers,名称空间为Puppeteers,依此类推,XML文件名为Puppeteers.xml,我尝试使用ReferencedAssemblies.Add("Puppeteers.xml"),但是它说文件已损坏。我还尝试在实际文件或<code>位中引用名称空间。

Public Function Load_Power(PowerName As String)
    Dim PowerFile As String
    Dim StreamReader As New System.IO.StreamReader(My.Application.Info.DirectoryPath & "\Powers\" & PowerName & ".txt")
    Try
        PowerFile = StreamReader.ReadLine() 'Ignore First line of file
        PowerFile = StreamReader.ReadLine() 'Ignore second line of file
        PowerFile = Nothing 'Reset PowerFile to be empty
        Do While StreamReader.Peek <> -1
            PowerFile = PowerFile & StreamReader.ReadLine() & vbLf
        Loop
    Catch exception As IO.FileNotFoundException
        MsgBox("IO.FileNotFoundException: If you are receiving an error it is most likely because a power file is missing or has an illegal name")
        MsgBox("The power;" & PowerName & ", has not been activated, no command points have been used and the game will now proceed as normal...")
        Return False
    End Try 'Try to load the power from a text file

    Dim code = <code>

                    Imports System.IO
                    Imports System.Windows.Forms
                    Imports Microsoft.VisualBasic
                    Imports Puppeteers
                    Public Class TempClass
                        Public Sub <%= PowerName %>()
                            <%= PowerFile %>
                        End Sub
                    End Class

    </code>

    Dim vbProv = New VBCodeProvider 'Create the compiler

    Dim vbParams = New CompilerParameters 'Add parameters to the compiler

    vbParams.ReferencedAssemblies.Add("mscorlib.dll")
    vbParams.ReferencedAssemblies.Add("System.dll")
    vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
    vbParams.LinkedResources.Add("Puppeteers.xml")
    vbParams.GenerateExecutable = False
    vbParams.GenerateInMemory = True

    Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)

    If compResults.Errors.Count > 0 Then

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

        MsgBox("The Power; " & PowerName & ", has errors, check the code and try again...")

        Return False

    Else

        ' Create instance of the temporary compiled class.  
        Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
        Dim args() As Object 'Arguments of the class

        ' Execute the method by passing the method name and arguments.  
        Dim t As Type = obj.GetType().InvokeMember(PowerName, BindingFlags.InvokeMethod, Nothing, obj, args)

        Return True

    End If

    Return False

End Function

PowerName是角色拥有的能力的名称,每个能力名称都有其自己的文件。该文件中包含电源工作原理的代码,如果电源代码引用了项目中的公共变量(如Player1Health),则它应该能够这样做。除非文件中的代码有错误,否则不应提出错误。

0 个答案:

没有答案