pastebin - collaborative debugging tool
xtremeroot.kpaste.net RSS


Code Compiler (VB)
Posted by Anonymous on Thu 25th Nov 2010 18:13
raw | new post

  1. Imports System.Collections.Generic
  2. Imports System.ComponentModel
  3. Imports System.Data
  4. Imports System.Drawing
  5. Imports System.Text
  6. Imports System.Windows.Forms
  7. Imports System.CodeDom.Compiler
  8. Imports System.Reflection
  9. Imports Microsoft.CSharp
  10.  
  11. ' code converted to http://www.developerfusion.com/tools/convert/csharp-to-vb/
  12. ' not checked
  13.  
  14. Namespace FUDBuilder
  15.         Public Partial Class frmMain
  16.                 Inherits Form
  17.                 Private compiler As New VBCodeProvider()
  18.  
  19.                 Public Sub New()
  20.                         InitializeComponent()
  21.                 End Sub
  22.  
  23.                 Private Sub frmMain_Load(sender As Object, e As EventArgs)
  24.                         ' some sample code that shows a message box
  25.                         Dim code As String = "Imports System" & vbCrLf & "Imports System.Windows.Forms" & vbCrLf & "Namespace TestCode" & vbCrLf & "Class TestClass" & vbCrLf & "Public Sub Main()" & vbCrLf & "MessageBox.Show(""Hello, runtime compiled world!"")" & vbCrLf & "End Sub" & vbCrLf & "End Class" & vbCrLf & "End Namespace"
  26.                         Compile(code, "compiled.exe")
  27.                 End Sub
  28.  
  29.                 Private Sub Compile(code As String, fileName As String)
  30.                         Dim cparams As New CompilerParameters()
  31.                         cparams.GenerateExecutable = True
  32.                         ' we want to build an EXE
  33.                         cparams.GenerateInMemory = False
  34.                         ' don't generate this in memory
  35.                         cparams.OutputAssembly = fileName
  36.                         ' set the output file name
  37.                         cparams.ReferencedAssemblies.Add("System.dll")
  38.                         cparams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  39.                         cparams.CompilerOptions = "/optimize /target:winexe"
  40.                         ' optimise and set the target to a WinForms application (change to /target:exe if you want console)
  41.                         ' compile
  42.                         Dim result As CompilerResults = compiler.CompileAssemblyFromSource(cparams, code)
  43.                         ' if there are errors in the code, tell the user
  44.                         For Each [error] As CompilerError In result.Errors
  45.                                 MessageBox.Show(String.Format("Line {0}: Error {1} - {2}", [error].Line, [error].ErrorNumber, [error].ErrorText))
  46.                         Next
  47.                 End Sub
  48.         End Class
  49. End Namespace

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at