Object arg = // Pass our textbox to the method Object obj = ("WinFormCodeCompile.Transform") StreamReader sReader = new input = sReader.ReadToEnd() ĬSharpCodeProvider cProv = new CSharpCodeProvider() ĬompilerParameters cParams = new CompilerParameters() Ĭ("mscorlib.dll") Ĭ("System.dll") Ĭ("") ĬompilerResults cResults = cProv.CompileAssemblyFromSource(cParams, code) Private void button1_Click(object sender, EventArgs e) You could also change the text to something a little more elaborate, such as: ' Execute the method by passing the method name and arguments.ĭim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args) ' An array of object that represent the arguments to be passed to our method (UpdateText). ' Create instance of the temporary compiled class.ĭim obj As Object = ("TempClass")
' Compile the code and get the compiler results (contains errors, etc.)ĭim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value) ' Ensure we generate an assembly in memory and not as a physical file. ' Create parameters to pass to the compiler. Public Sub UpdateText(ByVal txtOutput As TextBox) To start viewing messages, select the forum that you want to visit from the selection below. You may have to register before you can post: click the register link above to proceed. ' Notice the where the code read from the text file (Code.txt) If this is your first visit, be sure to check out the FAQ by clicking the link above. ' Create "code" literal to pass to the compiler. Private Sub btnPressMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPressMe.Clickĭim input = My.("Code.txt") This code is executed when the "Press Me" button is clicked. Create arguments to pass to our compiled codeĪll these steps are in order in the following code fragment.Create an instance of the class containing the code.Create compiler parameters and pass them to the compiler.Create an instance of a VB.NET compiler.There are a few simple steps we must complete to read, compile, and run the code. This is the code we will be compiling and running from our WinForms application. This file located in the project's bin directory (ensure you set it to "copy if newer") in the file properties section of Visual Studio. Here is the code contained in the text file (Code.txt). In this example, we will be reading properly formatted VB.NET code from a text file which will change the text of a TextBox in a Windows Forms application to "Hello vbCity!!" when a button is pressed.
Here is a simple tutorial on how to read, compile, and run code that is written in a text file.