EDN Admin
Well-known member
Im messing around with compiling & calling code on the fly. I created a form with a multiline textbox for code, a listbox for errors, a button to compile, and a button to quit.
Ive used the code to create an exefile, and that seemed to work well. But now i want to generate in memory, and call the assembly there. That is where i am having an issue. I do not know how to call the method.
The test code i am inputting at runtime is:
<div style="color:black; background-color:white
<pre><span style="color:blue Class a
<span style="color:blue Shared <span style="color:blue Sub Main
<span style="color:blue Dim b <span style="color:blue As <span style="color:blue Integer
b = 1
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue End <span style="color:blue Class
[/code]
<br/>
The code to compile this so far is:
<pre lang="x-vbnet Imports System.CodeDom.Compiler
Imports System.Reflection
Public Class Form1
Private Sub Compile_Click(sender As System.Object, e As System.EventArgs) Handles Compile.Click
Dim Asm As Assembly = Compile_The_Code()
Call it here....
Code_Text.Focus()
End Sub
Private Sub Quit_Click(sender As System.Object, e As System.EventArgs) Handles Quit.Click
End
End Sub
Private Function Compile_The_Code() As Assembly
Dim Provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
Dim Parameters As CompilerParameters = New CompilerParameters()
Dim Results As CompilerResults = Provider.CompileAssemblyFromSource(Parameters, Code_Text.Text)
If Results.Errors.HasErrors Then
Error_List.DataSource = Results.Errors
Compile_The_Code = Nothing
Else
Error_List.DataSource = {"No Errors"}
Compile_The_Code = Results.CompiledAssembly
End If
End Function
End Class[/code]
<br/>
<br/>
View the full article
Ive used the code to create an exefile, and that seemed to work well. But now i want to generate in memory, and call the assembly there. That is where i am having an issue. I do not know how to call the method.
The test code i am inputting at runtime is:
<div style="color:black; background-color:white
<pre><span style="color:blue Class a
<span style="color:blue Shared <span style="color:blue Sub Main
<span style="color:blue Dim b <span style="color:blue As <span style="color:blue Integer
b = 1
<span style="color:blue End <span style="color:blue Sub
<span style="color:blue End <span style="color:blue Class
[/code]
<br/>
The code to compile this so far is:
<pre lang="x-vbnet Imports System.CodeDom.Compiler
Imports System.Reflection
Public Class Form1
Private Sub Compile_Click(sender As System.Object, e As System.EventArgs) Handles Compile.Click
Dim Asm As Assembly = Compile_The_Code()
Call it here....
Code_Text.Focus()
End Sub
Private Sub Quit_Click(sender As System.Object, e As System.EventArgs) Handles Quit.Click
End
End Sub
Private Function Compile_The_Code() As Assembly
Dim Provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
Dim Parameters As CompilerParameters = New CompilerParameters()
Dim Results As CompilerResults = Provider.CompileAssemblyFromSource(Parameters, Code_Text.Text)
If Results.Errors.HasErrors Then
Error_List.DataSource = Results.Errors
Compile_The_Code = Nothing
Else
Error_List.DataSource = {"No Errors"}
Compile_The_Code = Results.CompiledAssembly
End If
End Function
End Class[/code]
<br/>
<br/>
View the full article