Click here to Skip to main content
15,868,420 members
Articles / Programming Languages / Visual Basic

Compiling with CodeDom

Rate me:
Please Sign up or sign in to vote.
4.48/5 (22 votes)
19 May 20042 min read 129.7K   2.3K   52  
This article explains how to compile code written in VB.NET or C# using the CodeDom.
Imports System.IO
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Collections.Specialized

Public Class VBNetCompiler

   'Files to compile
   Protected mFilesToCompile As New System.Collections.Specialized.StringCollection

   'Referenced assemblies
   Protected mImportedDlls As New StringCollection

   'Compiler output
   Protected mVBCompilerResults As CompilerResults
   'Compiler errores
   Protected mVBCompilerErrors As CompilerErrorCollection
   'Compiler parameters
   Protected mVBCompilerParameters As New CompilerParameters

   ''' <summary>
   ''' Constructor
   ''' </summary>
   ''' <param name="pFilesToCompile">Files to compile
   ''' </param>
   '''-----------------------------------------------------------------------------
   Public Sub New(ByVal ParamArray pFilesToCompile() As String)
      mFilesToCompile = New System.Collections.Specialized.StringCollection
      For Each oFile As String In pFilesToCompile
         mFilesToCompile.Add(oFile)
      Next
   End Sub

   '''-----------------------------------------------------------------------------
   ''' <summary>
   ''' Constructor
   ''' </summary>
   '''-----------------------------------------------------------------------------
   Public Sub New()
   End Sub

   '''-----------------------------------------------------------------------------
   ''' <summary>
   ''' Files to compile
   ''' </summary>
   '''-----------------------------------------------------------------------------
   Public Property FilesToCompile() As System.Collections.Specialized.StringCollection
      Get
         Return mFilesToCompile
      End Get
      Set(ByVal Value As System.Collections.Specialized.StringCollection)
         mFilesToCompile = Value
      End Set
   End Property

   '''-----------------------------------------------------------------------------
   ''' <summary>
   ''' Compiler parameters
   ''' </summary>
   '''-----------------------------------------------------------------------------
   Public Property VBCompilerParameters() As CompilerParameters
      Get
         Return mVBCompilerParameters
      End Get
      Set(ByVal Value As CompilerParameters)
         mVBCompilerParameters = Value
      End Set
   End Property

   ''' <summary>
   ''' Compiler errors
   ''' </summary>
   ''' <returns></returns>
   Public ReadOnly Property VBCompilerErrors() As CompilerErrorCollection
      Get
         Return mVBCompilerErrors
      End Get
   End Property

   ''' <summary>
   ''' Compiler output
   ''' </summary>
   ''' <returns></returns>
   Public ReadOnly Property VBCompilerResults() As CompilerResults
      Get
         Return mVBCompilerResults
      End Get
   End Property


   ''' <summary>
   ''' Referenced assemblies
   ''' </summary>
   ''' <returns></returns>
   Public Property ImportedDlls() As StringCollection
      Get
         Return mImportedDlls
      End Get
      Set(ByVal Value As StringCollection)
         mImportedDlls = Value
      End Set
   End Property


   ''' <summary>
   ''' Compiles the code
   ''' </summary>
   ''' <param name="pOutputPath"></param>
   Public Function Compile() As CompilerResults
      Dim oVBCodeProvider As New VBCodeProvider
      Dim oVBCodeCompiler As ICodeCompiler
      Dim oVBCompilerResults As CompilerResults
      Dim oVBCompilerError As CompilerError

      oVBCodeCompiler = oVBCodeProvider.CreateCompiler

      With mVBCompilerParameters
         With .ReferencedAssemblies
            For Each oDll As String In mImportedDlls
               ' adds assembly reference
               .Add(oDll)
            Next
         End With
      End With

      'Compiling process
      Dim oFiles() As String
      ReDim oFiles(mFilesToCompile.Count - 1)
      mFilesToCompile.CopyTo(oFiles, 0)
      oVBCompilerResults = oVBCodeCompiler.CompileAssemblyFromFileBatch(mVBCompilerParameters, oFiles)


      mVBCompilerResults = oVBCompilerResults
      With oVBCompilerResults
         Console.WriteLine("---------------------------------")
         Console.WriteLine("COMPILER OUTPUT: ")
         Console.WriteLine("---------------------------------")
         For Each oOut As String In .Output
            Console.WriteLine(oOut)
         Next
      End With

      With oVBCompilerResults
         Console.WriteLine("---------------------------------")
         Console.WriteLine("COMPILER ERRORS: " & .Errors.Count)
         Console.WriteLine("---------------------------------")

         mVBCompilerErrors = .Errors
         Dim ErrorLog As String
         For Each oVBCompilerError In .Errors
            ErrorLog += oVBCompilerError.ToString & vbNewLine
            Console.WriteLine(oVBCompilerError.ToString)
         Next
         If .Errors.Count > 0 Then
            Throw New Exception("The compiler was throwed the following errors:" & vbNewLine & ErrorLog)
         End If
      End With

      Return oVBCompilerResults
   End Function


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Argentina Argentina
Gustavo is Information Systems Enginner and
Information Systems Analyst. He born in Colonia Caroya, Cordoba, Argentine.
He worked in the Universidad Tecnologica Nacional in Cordoba, Argentine like Research Group Coordinator in the Business Intelligence area.

After that worked like a professor of Visual Basic, SQL Server 2000, .NET Plataform, Visual Basic.NET and C#

His experience in programming includes: QuickBasic, C, C++, FoxPro, Visual Basic, VB.NET, C# and ASP.NET. He has working with XML, XSD, XSLT and other related technologies.

He is now working in Pectra Technology, in the development and the software engineering area in a product named Pectra BPM Studio Framework (Business Process Management Solution) using Microsoft.NET and XML technologies.

His hobbies are play the piano and reading. He is a science lover

Comments and Discussions