Patrick_3 0 Light Poster

Hello, I am attempting to make a section in my application where a user can enter their own code which will be executed at runtime. I have looked up how to compile and run Visual Basic code while the program is running (Below).

Dim VBP As New VBCodeProvider
            Dim CVB As System.CodeDom.Compiler.ICodeCompiler

            CVB = VBP.CreateCompiler
            Dim PM As New System.CodeDom.Compiler.CompilerParameters

            PM.GenerateInMemory = True
            PM.GenerateExecutable = True
            PM.OutputAssembly = "RunCode.dll"
            PM.MainClass = "MainClass"
            PM.IncludeDebugInformation = True

            Dim ASM As System.Reflection.Assembly
            For Each ASM In AppDomain.CurrentDomain.GetAssemblies
                PM.ReferencedAssemblies.Add(ASM.Location)
            Next
            Dim CompileResults As System.CodeDom.Compiler.CompilerResults

        Dim srcText = "Imports Microsoft.VisualBasic" + Chr(13) _
                      + "Imports System.Windows.Forms" + Chr(13) _
                      + "Public Class MainClass" + Chr(13) _
                      + "Shared Sub Main()" + Chr(13) _
                      + Txt_Input.Text + Chr(13) _
                      + "End Sub" + Chr(13) _
                      + "End Class"
        CompileResults = CVB.CompileAssemblyFromSource(PM, srcText)

            Dim CompileErrors As System.CodeDom.Compiler.CompilerError

            For Each CompileErrors In CompileResults.Errors
                Txt_Output.AppendText(vbCrLf & CompileErrors.ErrorNumber & ": " & CompileErrors.ErrorText & ", " & CompileErrors.Line)
                Trace.WriteLine(Txt_Output.Text)
            Next

            Dim objRun As New Object
            Dim vArgs() As Object = Nothing

        objRun = CompileResults.CompiledAssembly.CreateInstance("MainCode.MainClass", False, Reflection.BindingFlags.CreateInstance, Nothing, vArgs, Nothing, Nothing)
        If Not objRun Is Nothing Then
            objRun.UserCode()
            'Dim oMethodInfo As Reflection.MethodInfo = objRun.GetType().GetMethod("Main")
            'Dim oRetObj As Object = oMethodInfo.Invoke(objRun, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic, Nothing, Nothing, Nothing) 'Find source dialog appears here
        Else
            For Each CompileError In CompileResults.Errors
                Trace.WriteLine("Compile Error:" + CompileError.ToString())
            Next
            MsgBox("Compile Error")
        End If

My specific application requires the user's code to be run multiple times with different values sent to it (a custom input data parser). How can I pass variables to the code that the user writes and by extension give them access to the values? For example, the user can use the inputString variable in their code and it will contain whatever was passed to the custom code.

I also need the user's code to be able to return a value (most likely in the form of a dictionary) or make changes to a variable already present in the application. How can this be accomplished?

If there is a more effecient/more direct way of accomplishing these tasks (basically a built-in scripting system that uses VB as the language) please tell me.

Thanks in advance