|
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.