Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 19-September-2015
- ' ***********************************************************************
- ' <copyright file="CodeDomUtil.vb" company="Elektro Studios">
- ' Copyright (c) Elektro Studios. All rights reserved.
- ' </copyright>
- ' ***********************************************************************
- #Region " Public Members Summary "
- #Region " Child Classes "
- ' CodeDomUtil.Compiler
- ' CodeDomUtil.Compiler.Warning
- ' CodeDomUtil.Compiler.Error
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs
- ' CodedomUtil.CompilerSettings
- ' CodeDomUtil.CompilerVersions
- ' CodeDomUtil.SubsystemVersions
- ' CodeDomUtil.VisualBasicCompiler : Inherits Compiler
- ' CodeDomUtil.CSharpCompiler : Inherits Compiler
- #End Region
- #Region " Variables / Constants / Read-Only "
- ' CodeDomUtil.Templates.TemplateVbConsoleApp
- ' CodeDomUtil.Templates.TemplateVbWinFormsApp
- ' CodeDomUtil.Templates.TemplateVbLib
- ' CodeDomUtil.Templates.TemplateCsConsoleApp
- ' CodeDomUtil.Templates.TemplateCsWinFormsApp
- ' CodeDomUtil.Templates.TemplateCsLib
- ' CodeDomUtil.CompilerVersions.V1 As String
- ' CodeDomUtil.CompilerVersions.V2 As String
- ' CodeDomUtil.CompilerVersions.V3 As String
- ' CodeDomUtil.CompilerVersions.V3_5 As String
- ' CodeDomUtil.CompilerVersions.V4 As String
- ' CodeDomUtil.SubsystemVersions.Windows2000
- ' CodeDomUtil.SubsystemVersions.WindowsXP
- ' CodeDomUtil.SubsystemVersions.WindowsServer2003
- ' CodeDomUtil.SubsystemVersions.WindowsVista
- ' CodeDomUtil.SubsystemVersions.Windows7
- ' CodeDomUtil.SubsystemVersions.WindowsServer2008
- ' CodeDomUtil.SubsystemVersions.Windows8
- ' CodeDomUtil.SubsystemVersions.Windows8_1
- ' CodeDomUtil.SubsystemVersions.Windows10
- #End Region
- #Region " Properties "
- ' CodeDomUtil.CompilerSettings.CodeProvider As CodeDomProvider
- ' CodeDomUtil.CompilerSettings.GenerateDebugInformation As Boolean
- ' CodeDomUtil.CompilerSettings.GenerateWarnings As Boolean
- ' CodeDomUtil.CompilerSettings.GenerateXmlDocumentation As Boolean
- ' CodeDomUtil.CompilerSettings.HighEntropyEnabled As Boolean
- ' CodeDomUtil.CompilerSettings.IntegerOverflowChecksEnabled As Boolean
- ' CodeDomUtil.CompilerSettings.LibraryPaths As List(Of String)
- ' CodeDomUtil.CompilerSettings.OptimizationsEnabled As Boolean
- ' CodeDomUtil.CompilerSettings.OutputLanguage As CultureInfo
- ' CodeDomUtil.CompilerSettings.Platform As CodeDomUtil.Platform
- ' CodeDomUtil.CompilerSettings.SubsystemVersion As String
- ' CodeDomUtil.CompilerSettings.TempFileCollection As TempFileCollection
- ' CodeDomUtil.CompilerSettings.TreatWarningsAsErrors As Boolean
- ' CodeDomUtil.CompilerSettings.Verbose As Boolean
- ' CodeDomUtil.CompilerSettings.VerboseSyntax As Boolean
- ' CodeDomUtil.CompilerSettings.WarningLevel As WarningLevelEnum
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.CodeDomProvider As CodeDomProvider
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.CompileErrors As IEnumerable(Of Compiler.Error)
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.CompilerParameters As CompilerParameters
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.CompilerWarnings As IEnumerable(Of Compiler.Warning)
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.SourceCode As String
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.SourceFilePath As String
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.TargetFilePath As String
- ' CodeDomUtil.Compiler.CompilerWorkDoneEventArgs.TempDirectoryPath As String
- #End Region
- #Region " Enumerations "
- ' CodeDomUtil.NetAssembly As Integer
- ' CodeDomUtil.WarningLevelEnum As Integer
- ' CodeDomUtil.Platform As Integer
- #End Region
- #Region " Events "
- ' CodeDomUtil.Compiler.CompilerWorkDone(Object, CodeDomUtil.Compiler.CompilerWorkDoneEventArgs)
- ' CodeDomUtil.VisualBasicCompiler.CompilerWorkDone(Object, CodeDomUtil.Compiler.CompilerWorkDoneEventArgs)
- ' CodeDomUtil.CSharpCompiler.CompilerWorkDone(Object, CodeDomUtil.Compiler.CompilerWorkDoneEventArgs)
- #End Region
- #Region " Methods "
- ' CodeDomUtil.Compiler.CompileFromString(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- ' CodeDomUtil.Compiler.CompileFromFile(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- ' CodeDomUtil.VisualBasicCompiler.CompileFromString(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- ' CodeDomUtil.VisualBasicCompiler.CompileFromFile(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- ' CodeDomUtil.CSharpCompiler.CompileFromString(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- ' CodeDomUtil.CSharpCompiler.CompileFromFile(CodeDomUtil.NetAssembly, String, String, String, Opt:IEnumerable(Of String), Opt:IEnumerable(Of String), Opt:String)
- #End Region
- #End Region
- #Region " Option Statements "
- Option Strict On
- Option Explicit On
- Option Infer Off
- #End Region
- #Region " Imports "
- Imports Microsoft.CSharp
- Imports Microsoft.VisualBasic
- Imports System
- Imports System.Collections.Generic
- Imports System.IO
- Imports System.CodeDom.Compiler
- Imports System.Linq
- #End Region
- #Region " CodeDom Util "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Contains related CodeDom utilities to compile .Net assemblies.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class CodeDomUtil
- #Region " Enumerations "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies a <see cref="CompilerParameters"></see> target assembly.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum NetAssembly As Integer
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' An executable console application or Command line interface (CLI).
- ''' The output file is created with an extension of .exe.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Console = 0
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' An executable Windows-based application or Graphical user interface (GUI).
- ''' The output file is created with an extension of .exe.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- WinExe = 1
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' An executable Windows-based application that must be run in an app container.
- ''' This setting is designed to be used for Windows 8.x Store applications.
- ''' The output file is created with an extension of .exe.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- WinExeContainer = 2
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' An intermediate file that you can convert to a Windows Runtime binary (.winmd) file.
- ''' The .winmd file can be consumed by JavaScript and C++ programs, in addition to managed language programs.
- ''' The output file name takes the name of the input file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- WinMdObj = 3
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A Dynamic-link library.
- ''' The output file is created with an extension of .dll.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- DynamicLinkLibrary = 4
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A module that can be added to an assembly.
- ''' The output file is created with an extension of .netmodule.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- [Module] = 5
- End Enum
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies the level at which the compiler should start displaying warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum WarningLevelEnum As Integer
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Turns off emission of all warning messages.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- None = 0
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Displays severe warning messages.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Level1 = 1
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Displays level 1 warnings plus certain, less-severe warnings,
- ''' such as warnings about hiding class members.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Level2 = 2
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Displays level 2 warnings plus certain, less-severe warnings,
- ''' such as warnings about expressions that always evaluate to true or false.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Level3 = 3
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Displays all level 3 warnings plus informational warnings.
- ''' This is the default warning level at the command line.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Level4 = 4
- End Enum
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Specifies a platform version of common language runtime (CLR).
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Enum Platform As Integer
- ''' <summary>
- ''' An assembly to run on any platform.
- ''' The application will run as a 32-bit application on 32-bit versions of Windows and as a 64-bit application on 64-bit versions of Windows.
- ''' This flag is the default value.
- ''' </summary>
- AnyCpu = 0
- ''' <summary>
- ''' An assembly to run on any platform.
- ''' The assembly will run as a 32-bit application on both 32-bit and 64-bit versions of Windows.
- ''' This flag is valid only for executables (.EXE) and requires .NET Framework 4.5.
- ''' </summary>
- AnyCpu32bitPreferred = 1
- ''' <summary>
- ''' An assembly to be run by the 32-bit, x86-compatible CLR.
- ''' </summary>
- X86 = 2
- ''' <summary>
- ''' An assembly to be run by the 64-bit CLR on a computer that supports the AMD64 or EM64T instruction set.
- ''' </summary>
- X64 = 3
- ''' <summary>
- ''' An assembly to be run by the 64-bit CLR on a computer with an Itanium processor.
- ''' </summary>
- Itanium = 4
- ''' <summary>
- ''' An assembly to be run on a computer with an ARM (Advanced RISC Machine) processor.
- ''' </summary>
- Arm = 5
- End Enum
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Prevents a default instance of the <see cref="CodeDomUtil"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- #End Region
- #Region " Child Classes "
- #Region " Templates "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Defines default application templates.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class Templates
- #Region " Constants / Read-Onlys "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a console application written in Visual Basic.Net.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateVbConsoleApp As String =
- <a><![CDATA[
- Imports System
- Namespace MainNamespace
- Module MainModule
- ''' <summary>
- ''' The main entry point for the application.
- ''' </summary>
- Sub Main()
- Console.WriteLine("VisualBasic.Net ConsoleApp Template.")
- End Sub
- End Module
- End Namespace
- ]]></a>.Value
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a WindowsForms application written in Visual Basic.Net.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateVbWinFormsApp As String =
- <a><![CDATA[
- Imports System
- Imports System.Windows.Forms
- Namespace MainNamespace
- Public NotInheritable Class MainClass : Inherits Form
- Public Sub New()
- Me.StartPosition = FormStartPosition.CenterScreen
- End Sub
- Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) _
- Handles MyBase.Shown
- MessageBox.Show("VisualBasic.Net WinForms Template.")
- End Sub
- End Class
- End Namespace
- ]]></a>.Value
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a library written in Visual Basic.Net.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateVbLib As String =
- <a><![CDATA[
- Imports System
- Namespace MainNamespace
- Public NotInheritable Class MainClass
- Private Sub New()
- End Sub
- Public Shared Function MyFunction(ByVal value As Object) As Object
- Return value
- End Function
- End Class
- End Namespace
- ]]></a>.Value
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a console application written in C#.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateCsConsoleApp As String =
- <a><![CDATA[
- using System;
- namespace MainNamespace
- {
- class MainClass
- {
- static void Main(string[] args)
- {
- Console.WriteLine("C# ConsoleApp Template.");
- }
- }
- }
- ]]></a>.Value
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a WindowsForms application written in C#.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateCsWinFormsApp As String =
- <a><![CDATA[
- using System;
- using System.Windows.Forms;
- namespace MainNamespace
- {
- public partial class MainClass : Form
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainClass());
- }
- public MainClass()
- {
- Shown += Form1_Shown;
- this.StartPosition = FormStartPosition.CenterScreen;
- }
- private void Form1_Shown(object sender, EventArgs e)
- {
- MessageBox.Show("C# WinForms Template.");
- }
- }
- }
- ]]></a>.Value
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' A sourcecode template of a library written in C#.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly TemplateCsLib As String =
- <a><![CDATA[
- using System;
- namespace MainNamespace
- {
- public sealed class MainClass
- {
- private MainClass()
- {}
- public static object MyFunction(object value)
- {
- return value;
- }
- }
- }
- ]]></a>.Value
- #End Region
- End Class
- #End Region
- #Region " Compiler Versions "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Exposes .NetFx compiler versions.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class CompilerVersions
- #Region " Constants "
- ''' <summary>
- ''' The .NetFx compiler version 1.
- ''' </summary>
- Public Const V1 As String = "1.0"
- ''' <summary>
- ''' The .NetFx compiler version 2.
- ''' </summary>
- Public Const V2 As String = "2.0"
- ''' <summary>
- ''' The .NetFx compiler version 3.
- ''' </summary>
- Public Const V3 As String = "3.0"
- ''' <summary>
- ''' The .NetFx compiler version 3.5.
- ''' </summary>
- Public Const V3_5 As String = "3.5"
- ''' <summary>
- ''' The .NetFx compiler version 4.
- ''' </summary>
- Public Const V4 As String = "4.0"
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Prevents a default instance of the <see cref="CompilerVersions"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Subsystem Versions "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Exposes Windows subsystem versions.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class SubsystemVersions
- #Region " Constants "
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const Windows2000 As String = "5.00"
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const WindowsXP As String = "5.01"
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const WindowsServer2003 As String = "5.02"
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const WindowsVista As String = "6.00"
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const Windows7 As String = "6.01"
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const WindowsServer2008 As String = SubsystemVersions.Windows7
- ''' <summary>
- ''' Windows 8 operating system.
- ''' </summary>
- Public Const Windows8 As String = "6.02"
- ''' <summary>
- ''' Windows 8.1 operating system.
- ''' </summary>
- Public Const Windows8_1 As String = SubsystemVersions.Windows8
- ''' <summary>
- ''' Windows 2000 operating system.
- ''' </summary>
- Public Const Windows10 As String = "10.00"
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Prevents a default instance of the <see cref="SubsystemVersions"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " CompilerSettings "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Defines the settings of a <see cref="CodeDomUtil.Compiler"/>
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class CompilerSettings
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the code provider.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The code provider.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property CodeProvider As CodeDomProvider
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the level at which the compiler should start displaying warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The level at which the compiler should start displaying warnings.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property WarningLevel As WarningLevelEnum = CodeDomUtil.WarningLevelEnum.Level3
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value instructs the compiler to treat warnings as errors.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if warnings are treated as errors; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property TreatWarningsAsErrors As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that instructs the compiler to generate debug information file (pdb).
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if debug information is enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property GenerateDebugInformation As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that instructs the compiler to generate Xml documentation file
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if Xml documentation is enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property GenerateXmlDocumentation As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that instructs the compiler to produce verbose status and error messages.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if verbose enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property Verbose As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that instructs the compiler from displaying code for syntax-related errors and warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if syntax-verbose enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property VerboseSyntax As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the language that the compiler will use to display the output messages.
- ''' This option is only avaliable for C# compiler.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The language that the compiler will use to display the output messages.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property OutputLanguage As CultureInfo
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that enables or disables compiler optimizations.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if compiler optimizations are enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property OptimizationsEnabled As Boolean = True
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that instructs the compiler to be able to generate warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if warnings are enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property GenerateWarnings As Boolean = True
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that indicates whether a 64-bit executable or an executable that's marked by the
- ''' <see cref="CodeDomUtil.Platform.AnyCpu"/> compiler option supports high entropy Address Space Layout Randomization (ASLR).
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if high entropy is enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property HighEntropyEnabled As Boolean = True
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that turns overflow-error checking for integer operations on or off.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>True</c> if integer overflow checks are enabled; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property IntegerOverflowChecksEnabled As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that specifies the minimum version of the subsystem on which the generated executable file can run,
- ''' thereby determining the versions of Windows on which the executable file can run.
- ''' Most commonly, this option ensures that the executable file can leverage particular security features that aren’t available
- ''' with older versions of Windows.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The subsystem on which the generated executable file can run.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property SubsystemVersion As String = CodeDomUtil.SubsystemVersions.Windows2000
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that specifies which platform version of common language runtime (CLR) can run the output file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The platform version of common language runtime (CLR) which can run the output file.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property Platform As CodeDomUtil.Platform = CodeDomUtil.Platform.AnyCpu
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that specifies additional directories in which to search for assembly references.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' A value that specifies additional directories in which to search for assembly references
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property LibraryPaths As List(Of String)
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the temporary files collection where the compiler stores the temporary files generated during a build.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The temporary files collection where the compiler stores the temporary files generated during a build.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property TempFileCollection As TempFileCollection
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="CompilerSettings"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Sub New()
- Me.LibraryPaths = New List(Of String)
- Me.OutputLanguage = New CultureInfo("en-US")
- Me.TempFileCollection = New TempFileCollection(tempdir:=Path.GetTempPath(), keepFiles:=True)
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Compiler "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Encapsulates a CodeDom provider compiler.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class Compiler
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the the settings of this <see cref="CodeDomUtil.Compiler"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The settings of this <see cref="CodeDomUtil.Compiler"/>
- ''' </value>.
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable Property Compilersettings As CompilerSettings
- #End Region
- #Region " Events "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Occurs when the compiler finishes.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Event CompilerWorkDone As EventHandler(Of CompilerWorkDoneEventArgs)
- #Region " CompilerWorkDone Args. "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Defines the event-data of a <see cref="CompilerWorkDone"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class CompilerWorkDoneEventArgs : Inherits EventArgs
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the <see cref="CodeDomProvider"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The <see cref="CodeDomProvider"/>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property CodeDomProvider As CodeDomProvider
- Get
- Return Me.codeDomProviderB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The <see cref="CodeDomProvider"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly codeDomProviderB As CodeDomProvider
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the compiler parameters used to build the target file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The compiler parameters used to build the target file.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property CompilerParameters As CompilerParameters
- Get
- Return Me.compilerParametersB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The compiler parameters used to build the target file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly compilerParametersB As CompilerParameters
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the compiler warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The compiler warnings.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property CompilerWarnings As IEnumerable(Of Compiler.Warning)
- Get
- Return Me.compilerWarningsB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The compiler warnings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly compilerWarningsB As IEnumerable(Of Compiler.Warning)
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the compiler errors.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The compiler errors.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property CompileErrors As IEnumerable(Of Compiler.Error)
- Get
- Return Me.compileErrorsB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The compiler errors.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly compileErrorsB As IEnumerable(Of Compiler.Error)
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the source code string. Only when a string is used to compile.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The source code string. Only when a string is used to compile.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property SourceCode As String
- Get
- Return Me.sourceCodeB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The source code string.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly sourceCodeB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the source filepath. Only when a file is used to compile.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The source filepath. Only when a file is used to compile.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property SourceFilePath As String
- Get
- Return Me.sourceFilePathB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The source filepath. Only when a file is used to compile.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly sourceFilePathB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the target .Net assembly filepath.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The target .Net assembly filepath.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property TargetFilePath As String
- Get
- Return Me.targetFilePathB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The target .Net assembly filepath.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly targetFilePathB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the temporary files path used for the compiler.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The temporary files path used for the compiler.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property TempDirectoryPath As String
- Get
- Return Me.tempDirectoryPathB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The temporary files path used for the compiler.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly tempDirectoryPathB As String
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="CompilerWorkDoneEventArgs"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="codeDomProvider">
- ''' The CodeDom provider.
- ''' </param>
- '''
- ''' <param name="compilerParameters">
- ''' The compiler parameters used to build the target file.
- ''' </param>
- '''
- ''' <param name="compilerWarnings">
- ''' The compiler warnings.
- ''' </param>
- '''
- ''' <param name="compileErrors">
- ''' The compile errors.
- ''' </param>
- '''
- ''' <param name="sourceCode">
- ''' The source code string (if any).
- ''' </param>
- '''
- ''' <param name="sourceFilePath">
- ''' The source file path (if any).
- ''' </param>
- '''
- ''' <param name="targetFilePath">
- ''' The target .Net assembly filepath.
- ''' </param>
- '''
- ''' <param name="tempDirectoryPath">
- ''' The temporary files path used for the compiler.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New(ByVal codeDomProvider As CodeDomProvider,
- ByVal compilerParameters As CompilerParameters,
- ByVal compilerWarnings As IEnumerable(Of Compiler.Warning),
- ByVal compileErrors As IEnumerable(Of Compiler.Error),
- ByVal sourceCode As String,
- ByVal sourceFilePath As String,
- ByVal targetFilePath As String,
- ByVal tempDirectoryPath As String)
- Me.codeDomProviderB = codeDomProvider
- Me.compilerParametersB = compilerParameters
- Me.compilerWarningsB = compilerWarnings
- Me.compileErrorsB = compileErrors
- Me.sourceCodeB = sourceCode
- Me.sourceFilePathB = sourceFilePath
- Me.targetFilePathB = targetFilePath
- Me.tempDirectoryPathB = tempDirectoryPath
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="CompilerWorkDoneEventArgs"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- #End Region
- End Class
- #End Region
- #End Region
- #Region " Constructors "
- ''' <summary>
- ''' Initializes a new instance of the <see cref="Compiler"/> class.
- ''' </summary>
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New()
- Me.Compilersettings = New CompilerSettings
- End Sub
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Compiles a .Net assembly from the specified source code string.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="netAssembly">
- ''' The target kind of .Net assembly.
- ''' </param>
- '''
- ''' <param name="targetFile">
- ''' The target local file to create.
- ''' </param>
- '''
- ''' <param name="sourceCode">
- ''' The input source code to compile.
- ''' </param>
- '''
- ''' <param name="mainMemberName">
- ''' The member name of the main entry point.
- ''' </param>
- '''
- ''' <param name="referencedAssemblies">
- ''' The referenced assemblies.
- ''' </param>
- '''
- ''' <param name="resources">
- ''' The managed .Net resources.
- ''' </param>
- '''
- ''' <param name="iconFile">
- ''' The icon used to set the appereance of the executable file.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Exception">
- ''' The current CodeDomProvider does not support resource embedding.
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Sub CompileFromString(ByVal netAssembly As NetAssembly,
- ByVal targetFile As String,
- ByVal sourceCode As String,
- ByVal mainMemberName As String,
- Optional ByVal referencedAssemblies As IEnumerable(Of String) = Nothing,
- Optional ByVal resources As IEnumerable(Of String) = Nothing,
- Optional ByVal iconFile As String = "")
- Dim cProvider As CodeDomProvider = Me.Compilersettings.CodeProvider
- Dim cParams As New CompilerParameters
- Dim cResult As CompilerResults
- Me.InitializeCompilerSettings(cProvider, cParams, netAssembly,
- targetFile, mainMemberName,
- referencedAssemblies, resources, iconFile)
- cResult = cProvider.CompileAssemblyFromSource(cParams, sourceCode)
- Me.FireCompilerWorkDone(Me, cProvider, cParams, cResult,
- targetFile, sourceCode:=sourceCode)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Compiles a .Net assembly from the specified source code file.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="netAssembly">
- ''' The target kind of .Net assembly.
- ''' </param>
- '''
- ''' <param name="targetFile">
- ''' The target local file to create.
- ''' </param>
- '''
- ''' <param name="sourceFile">
- ''' The input source file to compile.
- ''' </param>
- '''
- ''' <param name="mainMemberName">
- ''' The member name of the main entry point.
- ''' </param>
- '''
- ''' <param name="referencedAssemblies">
- ''' The referenced assemblies.
- ''' </param>
- '''
- ''' <param name="resources">
- ''' The managed .Net resources.
- ''' </param>
- '''
- ''' <param name="iconFile">
- ''' The icon used to set the appereance of the executable file.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Exception">
- ''' The current CodeDomProvider does not support resource embedding.
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Sub CompileFromFile(ByVal netAssembly As NetAssembly,
- ByVal targetFile As String,
- ByVal sourceFile As String,
- ByVal mainMemberName As String,
- Optional ByVal referencedAssemblies As IEnumerable(Of String) = Nothing,
- Optional ByVal resources As IEnumerable(Of String) = Nothing,
- Optional ByVal iconFile As String = "")
- Dim cProvider As CodeDomProvider = Me.Compilersettings.CodeProvider
- Dim cParams As New CompilerParameters
- Dim cResult As CompilerResults
- Me.InitializeCompilerSettings(cProvider, cParams, netAssembly,
- targetFile, mainMemberName,
- referencedAssemblies, resources, iconFile)
- cResult = cProvider.CompileAssemblyFromFile(cParams, sourceFile)
- Me.FireCompilerWorkDone(Me, cProvider, cParams, cResult,
- targetFile, sourceFile:=sourceFile)
- End Sub
- #End Region
- #Region " Private Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets a compiler options string in specific language syntax.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="targetAssembly">
- ''' The target kind of .Net assembly.
- ''' </param>
- '''
- ''' <param name="compilerSettings">
- ''' The <see cref="CodeDomUtil.CompilerSettings"/> instance that encapsulates the compiler options.
- ''' </param>
- '''
- ''' <param name="iconFile">
- ''' The icon used to set the appereance of the executable file.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' A <see cref="String"/> that contains the compiler options in specific language syntax.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Function GetCompilerOptions(ByVal targetAssembly As NetAssembly,
- ByVal compilerSettings As CompilerSettings,
- Optional ByVal iconFile As String = "") As String
- Dim compilerParams As New List(Of String)
- ' VB: http://msdn.microsoft.com/en-us/library/31xs5fhx.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/6h25dztx.aspx
- Select Case targetAssembly
- Case NetAssembly.Console
- compilerParams.Add("/target:exe")
- Case NetAssembly.WinExe
- compilerParams.Add("/target:winexe")
- Case NetAssembly.WinExeContainer
- compilerParams.Add("/target:appcontainerexe")
- Case NetAssembly.WinMdObj
- compilerParams.Add("/target:winmdobj")
- Case NetAssembly.DynamicLinkLibrary
- compilerParams.Add("/target:library")
- Case NetAssembly.Module
- compilerParams.Add("/target:module")
- Case Else
- End Select
- ' VB: http://msdn.microsoft.com/en-us/library/8ck8e1y2.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/t0hfscdc.aspx
- compilerParams.Add(String.Format("/platform:{0}", compilerSettings.Platform.ToString.ToLower))
- ' VB: http://msdn.microsoft.com/en-us/library/etx40x86.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/8cw0bt21.aspx
- If Me.Compilersettings.GenerateDebugInformation Then
- compilerParams.Add("/debug:pdbonly")
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/f64ezf9b.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/3260k4x7.aspx
- Select Case True
- Case TypeOf compilerSettings.CodeProvider Is CSharpCodeProvider
- If compilerSettings.IntegerOverflowChecksEnabled Then
- compilerParams.Add("/doc:")
- End If
- Case TypeOf compilerSettings.CodeProvider Is VBCodeProvider
- If Me.Compilersettings.GenerateXmlDocumentation Then
- compilerParams.Add("/doc+")
- Else
- compilerParams.Add("/doc-")
- End If
- End Select
- ' VB: http://msdn.microsoft.com/en-us/library/hh500470.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/hh156527.aspx
- If compilerSettings.HighEntropyEnabled Then
- compilerParams.Add("/highentropyva+")
- Else
- compilerParams.Add("/highentropyva-")
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/key2x70f.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx
- Select Case True
- Case TypeOf compilerSettings.CodeProvider Is CSharpCodeProvider
- If compilerSettings.IntegerOverflowChecksEnabled Then
- compilerParams.Add("/checked+")
- Else
- compilerParams.Add("/checked-")
- End If
- Case TypeOf compilerSettings.CodeProvider Is VBCodeProvider
- If compilerSettings.IntegerOverflowChecksEnabled Then
- compilerParams.Add("/removeintchecks+")
- Else
- compilerParams.Add("/removeintchecks-")
- End If
- End Select
- ' VB: http://msdn.microsoft.com/en-us/library/yf8493s5.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/t0hfscdc.aspx
- If compilerSettings.OptimizationsEnabled Then
- compilerParams.Add("/optimize+")
- Else
- compilerParams.Add("/optimize-")
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/jj127108.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/hh965708.aspx
- compilerParams.Add(String.Format("/subsystemversion:{0}", compilerSettings.SubsystemVersion))
- ' VB: http://msdn.microsoft.com/en-us/library/c86sssa5.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/7f28x9z3.aspx
- If Not compilerSettings.GenerateWarnings Then
- compilerParams.Add("/nowarn")
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/2xz9dxe5.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/406xhdz3.aspx
- If Me.Compilersettings.TreatWarningsAsErrors Then
- compilerParams.Add("/warnaserror+")
- Else
- compilerParams.Add("/warnaserror-")
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/yafz4da8.aspx
- If TypeOf compilerSettings.CodeProvider Is VBCodeProvider Then
- If compilerSettings.Verbose Then
- compilerParams.Add("/verbose+")
- Else
- compilerParams.Add("/verbose-")
- End If
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/ay1354fe.aspx
- If (Not compilerSettings.VerboseSyntax) AndAlso (TypeOf compilerSettings.CodeProvider Is VBCodeProvider) Then
- compilerParams.Add("/quiet")
- End If
- ' C#: https://msdn.microsoft.com/en-us/library/hh506932.aspx
- If (TypeOf compilerSettings.CodeProvider Is CSharpCodeProvider) Then
- compilerParams.Add(String.Format("/preferreduilang:{0}", compilerSettings.OutputLanguage.Name))
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/683be552.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/2aaxe43f.aspx
- If Not String.IsNullOrEmpty(iconFile) Then
- compilerParams.Add(String.Format("/win32icon:""{0}""", iconFile))
- End If
- ' VB: http://msdn.microsoft.com/en-us/library/4sw42s7k.aspx
- ' C#: http://msdn.microsoft.com/en-us/library/s5bac5fx.aspx
- If compilerSettings.LibraryPaths.Any Then
- Select Case True
- Case TypeOf compilerSettings.CodeProvider Is CSharpCodeProvider
- compilerParams.Add(String.Format("/lib:{0}",
- String.Join(";", From dirPath As String In compilerSettings.LibraryPaths
- Select String.Format("""{0}""", dirPath))))
- Case TypeOf compilerSettings.CodeProvider Is VBCodeProvider
- compilerParams.Add(String.Format("/libpath:{0}",
- String.Join(";", From dirPath As String In compilerSettings.LibraryPaths
- Select String.Format("""{0}""", dirPath))))
- End Select
- End If
- Return String.Join(" ", compilerParams)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes the compiler settings.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="cProvider">
- ''' The <see cref="CodeDomProvider"/> instance.
- ''' </param>
- '''
- ''' <param name="cParams">
- ''' The <see cref="CompilerParameters"/> instance.
- ''' </param>
- '''
- ''' <param name="netAssembly">
- ''' The target kind of .Net assembly.
- ''' </param>
- '''
- ''' <param name="targetFile">
- ''' The target local file to create.
- ''' </param>
- '''
- ''' <param name="mainMemberName">
- ''' The member name of the main entry point.
- ''' </param>
- '''
- ''' <param name="referencedAssemblies">
- ''' The referenced assemblies.
- ''' </param>
- '''
- ''' <param name="resources">
- ''' The managed .Net resources.
- ''' </param>
- '''
- ''' <param name="iconFile">
- ''' The icon used to set the appereance of the executable file.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="Exception">
- ''' The current CodeDomProvider does not support resource embedding
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Sub InitializeCompilerSettings(ByRef cProvider As CodeDomProvider,
- ByRef cParams As CompilerParameters,
- ByVal netAssembly As NetAssembly,
- ByVal targetFile As String,
- ByVal mainMemberName As String,
- Optional ByVal referencedAssemblies As IEnumerable(Of String) = Nothing,
- Optional ByVal resources As IEnumerable(Of String) = Nothing,
- Optional ByVal iconFile As String = "")
- ' Renew TempFileCollection.
- If Me.Compilersettings.TempFileCollection.KeepFiles Then
- Me.Compilersettings.TempFileCollection = New TempFileCollection(tempdir:=Me.Compilersettings.TempFileCollection.TempDir, keepFiles:=Me.Compilersettings.TempFileCollection.KeepFiles)
- End If
- ' Set a default assembly reference.
- If (referencedAssemblies Is Nothing) OrElse (Not referencedAssemblies.Any) Then
- referencedAssemblies = {"System.dll"}
- End If
- ' Set compiler parameters.
- cParams.CompilerOptions = Me.GetCompilerOptions(netAssembly, Me.Compilersettings, iconFile)
- With cParams
- Select Case netAssembly
- Case CodeDomUtil.NetAssembly.Console, CodeDomUtil.NetAssembly.WinExe, CodeDomUtil.NetAssembly.WinExeContainer
- .GenerateExecutable = True
- Case CodeDomUtil.NetAssembly.DynamicLinkLibrary, CodeDomUtil.NetAssembly.Module, CodeDomUtil.NetAssembly.WinMdObj
- .GenerateExecutable = False
- Case Else
- ' Throw New NotImplementedException("Bad NetAssembly value.")
- End Select
- .GenerateInMemory = False ' Save the assembly as a physical file.
- .OutputAssembly = targetFile
- .ReferencedAssemblies.AddRange(referencedAssemblies.ToArray)
- .TempFiles = Me.Compilersettings.TempFileCollection
- .WarningLevel = Me.Compilersettings.WarningLevel
- End With
- ' Set the embedded resource file of the assembly.
- If cProvider.Supports(GeneratorSupport.Resources) AndAlso (resources IsNot Nothing) Then
- cParams.EmbeddedResources.AddRange(resources.ToArray)
- ElseIf (Not cProvider.Supports(GeneratorSupport.Resources)) AndAlso (resources IsNot Nothing) Then
- Throw New Exception(message:="The current CodeDomProvider does not support resource embedding.")
- End If
- ' Specify the class/module that contains the main method of the source code.
- If cProvider.Supports(GeneratorSupport.EntryPointMethod) Then
- cParams.MainClass = mainMemberName
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Fires the <see cref="CompilerWorkDone"/> event with the specified event data.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source of the event.
- ''' </param>
- '''
- ''' <param name="cProvider">
- ''' The <see cref="CodeDomProvider"/> instance.
- ''' </param>
- '''
- ''' <param name="cParams">
- ''' The <see cref="CompilerParameters"/> instance.
- ''' </param>
- '''
- ''' <param name="cResult">
- ''' The <see cref="CompilerResults"/> instance.
- ''' </param>
- '''
- ''' <param name="targetFile">
- ''' The target .Net assembly file.
- ''' </param>
- '''
- ''' <param name="sourceCode">
- ''' The source code (if any).
- ''' </param>
- '''
- ''' <param name="sourceFile">
- ''' The source file (if any).
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- Protected Overridable Sub FireCompilerWorkDone(ByVal sender As Object,
- ByVal cProvider As CodeDomProvider,
- ByVal cParams As CompilerParameters,
- ByVal cResult As CompilerResults,
- ByVal targetFile As String,
- Optional ByVal sourceCode As String = "",
- Optional ByVal sourceFile As String = "")
- If Me.CompilerWorkDoneEvent IsNot Nothing Then
- Dim compilerWarnings As IEnumerable(Of Compiler.Warning) =
- From ce As CompilerError In cResult.Errors.Cast(Of CompilerError)()
- Where ce.IsWarning
- Select New Compiler.Warning(ce)
- Dim compileErrors As IEnumerable(Of Compiler.Error) =
- From ce As CompilerError In cResult.Errors.Cast(Of CompilerError)()
- Where Not ce.IsWarning
- Select New Compiler.Error(ce)
- Dim eventData As New CompilerWorkDoneEventArgs(codeDomProvider:=cProvider,
- compilerParameters:=cParams,
- compilerWarnings:=compilerWarnings,
- compileErrors:=compileErrors,
- sourceCode:=sourceCode,
- sourceFilePath:=sourceFile,
- targetFilePath:=targetFile,
- tempDirectoryPath:=cParams.TempFiles.TempDir)
- RaiseEvent CompilerWorkDone(sender, eventData)
- End If
- End Sub
- #End Region
- #Region " Child Classes "
- #Region " Warning "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Represents a compiler warning.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class Warning : Inherits CompilerError
- #Region " Hidden Base Members "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that indicates whether the error is a warning.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>tTrue</c> if this instance is warning; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- Friend Overridable Shadows ReadOnly Property IsWarning As Boolean
- Get
- Return MyBase.IsWarning
- End Get
- End Property
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="Warning"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="ce">
- ''' A <see cref="CodeDom.Compiler.CompilerError"/> instance that contains the warning data.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- Public Sub New(ByVal ce As CompilerError)
- MyBase.New(ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText)
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Error "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Represents a compiler error.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class [Error] : Inherits CompilerError
- #Region " Hidden Base Members "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that indicates whether the error is a warning.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' <c>tTrue</c> if this instance is warning; otherwise, <c>False</c>.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- Friend Overridable Shadows ReadOnly Property IsWarning As Boolean
- Get
- Return MyBase.IsWarning
- End Get
- End Property
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="[Error]"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="ce">
- ''' A <see cref="CodeDom.Compiler.CompilerError"/> instance that contains the error data.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- Public Sub New(ByVal ce As CompilerError)
- MyBase.New(ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText)
- End Sub
- #End Region
- End Class
- #End Region
- #End Region
- End Class
- #End Region
- #Region " Visual Basic Compiler "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Provides Visual Basic code compiler features.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example that demonstrates how to use the VisualBasicCompiler.
- ''' <code>
- ''' Public NotInheritable Class Form1 : Inherits Form
- '''
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <summary>
- ''' ''' The VisualBasic.Net compiler instance.
- ''' ''' </summary>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' Dim WithEvents vbCompiler As CodeDomUtil.Compiler =
- ''' New CodeDomUtil.VisualBasicCompiler(CodeDomUtil.CompilerVersions.V4)
- '''
- ''' Private Sub Form1_Shown() Handles MyBase.Shown
- '''
- ''' With Me.vbCompiler.Compilersettings
- ''' .GenerateDebugInformation = True
- ''' .GenerateWarnings = True
- ''' .GenerateXmlDocumentation = True
- ''' .HighEntropyEnabled = True
- ''' .IntegerOverflowChecksEnabled = False
- ''' .OptimizationsEnabled = True
- ''' .Platform = CodeDomUtil.Platform.AnyCpu
- ''' .SubsystemVersion = CodeDomUtil.SubsystemVersions.WindowsXP
- ''' .TreatWarningsAsErrors = False
- ''' .Verbose = True
- ''' .VerboseSyntax = False
- ''' .WarningLevel = CodeDomUtil.WarningLevelEnum.Level3
- ''' .LibraryPaths.Add(IO.Directory.GetCurrentDirectory)
- ''' End With
- '''
- ''' Dim referencedAssemblies As New List(Of String)
- ''' referencedAssemblies.AddRange({"System.dll", "System.Windows.Forms.dll"})
- '''
- ''' ' Compile a VB Console App from string.
- ''' vbCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.Console,
- ''' targetFile:="C:\VB Default Console App.exe",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateVbConsoleApp,
- ''' mainMemberName:="MainNamespace.MainModule",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a VB WinForms App from string.
- ''' vbCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.WinExe,
- ''' targetFile:="C:\VB Default WinForms App.exe",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateVbWinFormsApp,
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a VB library from string.
- ''' vbCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.DynamicLinkLibrary,
- ''' targetFile:="C:\VB Default Library.dll",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateVbLib,
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a VB local file that contains the sourcecode.
- ''' vbCompiler.CompileFromFile(netAssembly:=CodeDomUtil.NetAssembly.WinExe,
- ''' targetFile:="C:\VB Custom App.exe",
- ''' sourceFile:="C:\SourceCode.vb",
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' End Sub
- '''
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <summary>
- ''' ''' Handles the <see cref="CodeDomUtil.Compiler.CompilerWorkDone"/> event of the vbCompiler instance.
- ''' ''' </summary>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <param name="sender">
- ''' ''' The source of the event.
- ''' ''' </param>
- ''' '''
- ''' ''' <param name="e">
- ''' ''' The <see cref="CodeDomUtil.Compiler.CompilerWorkDoneEventArgs"/> instance containing the event data.
- ''' ''' </param>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' Public Sub VbCompiler_CompilerWorkDone(ByVal sender As Object, ByVal e As CodeDomUtil.Compiler.CompilerWorkDoneEventArgs) _
- ''' Handles vbCompiler.CompilerWorkDone
- '''
- ''' Console.WriteLine(String.Format("Compiler: {0}", e.CodeDomProvider.ToString))
- ''' Console.WriteLine(String.Format("Parameters: {0}", e.CompilerParameters.CompilerOptions))
- '''
- ''' For Each war As CodeDomUtil.Compiler.Warning In e.CompilerWarnings
- ''' Console.WriteLine(String.Format("{0}| Warning: {1}", war.ErrorNumber, war.ErrorText))
- ''' Next war
- '''
- ''' For Each err As CodeDomUtil.Compiler.Error In e.CompileErrors
- ''' Console.WriteLine(String.Format("{0}| Error: {1}", err.ErrorNumber, err.ErrorText))
- ''' Next err
- '''
- ''' If Not e.CompileErrors.Any Then
- ''' Console.WriteLine(String.Format("Compilation Successful: {0}", e.TargetFilePath))
- ''' End If
- '''
- ''' Console.WriteLine()
- '''
- ''' End Sub
- '''
- ''' End Class
- ''' </code>
- ''' </example>
- Public NotInheritable Class VisualBasicCompiler : Inherits Compiler
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="VisualBasicCompiler"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="version">
- ''' The .NetFx compiler version.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New(Optional version As String = "")
- Dim providerOptions As New Dictionary(Of String, String)()
- If Not String.IsNullOrEmpty(version) Then
- providerOptions.Add("CompilerVersion", String.Format("v{0}", version))
- End If
- MyBase.Compilersettings.CodeProvider = New VBCodeProvider(providerOptions)
- End Sub
- #End Region
- End Class
- #End Region
- #Region " CSharp Compiler "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Provides CSharp code compiler features.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example that demonstrates how to use the CSharpCompiler.
- ''' <code>
- ''' Public NotInheritable Class Form1 : Inherits Form
- '''
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <summary>
- ''' ''' The C# compiler instance.
- ''' ''' </summary>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' Dim WithEvents csCompiler As CodeDomUtil.Compiler =
- ''' New CodeDomUtil.CSharpCompiler(CodeDomUtil.CompilerVersions.V4)
- '''
- ''' Private Sub Form1_Shown() Handles MyBase.Shown
- '''
- ''' With Me.csCompiler.Compilersettings
- ''' .GenerateDebugInformation = True
- ''' .GenerateWarnings = True
- ''' .GenerateXmlDocumentation = True
- ''' .HighEntropyEnabled = True
- ''' .IntegerOverflowChecksEnabled = False
- ''' .OptimizationsEnabled = True
- ''' .OutputLanguage = New CultureInfo("en-US")
- ''' .Platform = CodeDomUtil.Platform.AnyCpu
- ''' .SubsystemVersion = CodeDomUtil.SubsystemVersions.WindowsXP
- ''' .TreatWarningsAsErrors = False
- ''' .Verbose = True
- ''' .VerboseSyntax = False
- ''' .WarningLevel = CodeDomUtil.WarningLevelEnum.Level3
- ''' .LibraryPaths.Add(IO.Directory.GetCurrentDirectory)
- ''' End With
- '''
- ''' Dim referencedAssemblies As New List(Of String)
- ''' referencedAssemblies.AddRange({"System.dll", "System.Windows.Forms.dll"})
- '''
- ''' ' Compile a C# Console App from string.
- ''' csCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.Console,
- ''' targetFile:="C:\CS Default Console App.exe",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateCsConsoleApp,
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a C# WinForms App from string.
- ''' csCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.WinExe,
- ''' targetFile:="C:\CS Default WinForms App.exe",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateCsWinFormsApp,
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a C# library from string.
- ''' csCompiler.CompileFromString(netAssembly:=CodeDomUtil.NetAssembly.DynamicLinkLibrary,
- ''' targetFile:="C:\CS Default Library.dll",
- ''' sourceCode:=CodeDomUtil.Templates.TemplateCsLib,
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' ' Compile a C# local file that contains the sourcecode.
- ''' csCompiler.CompileFromFile(netAssembly:=CodeDomUtil.NetAssembly.WinExe,
- ''' targetFile:="C:\CS Custom App.exe",
- ''' sourceFile:="C:\SourceCode.cs",
- ''' mainMemberName:="MainNamespace.MainClass",
- ''' referencedAssemblies:=referencedAssemblies,
- ''' resources:=Nothing,
- ''' iconFile:=Nothing)
- '''
- ''' End Sub
- '''
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <summary>
- ''' ''' Handles the <see cref="CodeDomUtil.Compiler.CompilerWorkDone"/> event of the csCompiler instance.
- ''' ''' </summary>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <param name="sender">
- ''' ''' The source of the event.
- ''' ''' </param>
- ''' '''
- ''' ''' <param name="e">
- ''' ''' The <see cref="CodeDomUtil.Compiler.CompilerWorkDoneEventArgs"/> instance containing the event data.
- ''' ''' </param>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' Public Sub CsCompiler_CompilerWorkDone(ByVal sender As Object, ByVal e As CodeDomUtil.Compiler.CompilerWorkDoneEventArgs) _
- ''' Handles csCompiler.CompilerWorkDone
- '''
- ''' Console.WriteLine(String.Format("Compiler: {0}", e.CodeDomProvider.ToString))
- ''' Console.WriteLine(String.Format("Parameters: {0}", e.CompilerParameters.CompilerOptions))
- '''
- ''' For Each war As CodeDomUtil.Compiler.Warning In e.CompilerWarnings
- ''' Console.WriteLine(String.Format("{0}| Warning: {1}", war.ErrorNumber, war.ErrorText))
- ''' Next war
- '''
- ''' For Each err As CodeDomUtil.Compiler.Error In e.CompileErrors
- ''' Console.WriteLine(String.Format("{0}| Error: {1}", err.ErrorNumber, err.ErrorText))
- ''' Next err
- '''
- ''' If Not e.CompileErrors.Any Then
- ''' Console.WriteLine(String.Format("Compilation Successful: {0}", e.TargetFilePath))
- ''' End If
- '''
- ''' Console.WriteLine()
- '''
- ''' End Sub
- '''
- ''' End Class
- ''' </code>
- ''' </example>
- Public NotInheritable Class CSharpCompiler : Inherits Compiler
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="CSharpCompiler"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="version">
- ''' The .NetFx compiler version.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New(Optional version As String = "")
- Dim providerOptions As New Dictionary(Of String, String)()
- If Not String.IsNullOrEmpty(version) Then
- providerOptions.Add("CompilerVersion", String.Format("v{0}", version))
- End If
- MyBase.Compilersettings.CodeProvider = New CSharpCodeProvider(providerOptions)
- End Sub
- #End Region
- End Class
- #End Region
- #End Region
- End Class
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement