Dec 19, 2011 07:51 AM
(This post was last modified: Dec 19, 2011 08:09 AM by Skillful.)
Hello.
I am glad to present my tutorial! :)
This tutorial will teach you to use the CodeDom Compiler in VB.NET
Before you start reading this tutorial I would recommend you to check out this program so that you know what can you do by reading this tutorial.
Its nice to be able to make programs with your own programs. You can customize it how you want and it dont need to have that big file size like VB.NET Express or Visual Studio. So im going to teach you how to:
So lets start! :D
CodeDom is Code Document Object Model. This provides a graphical (in the sense of graph nodes, not of a picture) representation of code in the form of a tree. This represents the hierarchy of nested code elements (namespaces contain classes, classes contain methods, methods contain variable declarations, and so on). You can use CodeDom for generating an in memory code tree, or you can use it to compile ordinary code written in a file (like we will do later). View MSDN to read all about CodeDom elements. With this code tree, you can represent any piece of code independently of a specific language.
To begin with...
Import this namespace at the top of your code:
Inside your class, you need to declare these variables:
In this tutorial we will be using Options as CompilerParameters and Provider as a CodeDomProvider.
To create a compiler, you must choose what language that shall be used, for example VB, CS, CPP, etc.
To do this, use this code:
Of course you can change "vb" to whatever coding language you want, but we will come back to that later in Show installed Programming Languages.
We will use the class CompilerParameters to set the options for our compiler.
The available options are:
To add references to your project, use the ReferencedAssemblies property.
This example demonstrates how to use this property:
Its smart to always add the System.dll reference because i cant think of any program that don't uses that. Mscorlib.dll is also a smart reference to add. To do so use this:
Resources is a group of files compiled into a .NET Resource file. The can be images, sounds, text, or files. They are used by the program in different ways. One way is to display an image in a picture box, another is to change the language of the application. The EmbeddedResources property is mostly files or objects used in the application, while the LinkedResources can be culture options, these will be included in a separate assembly. To check if the assembly supports resources, use this piece of code:
To add a resource file, or more use this:
You can change 'EmbeddedResources' to 'LinkedResources' if you want the resources to be linked in another assembly.
Its handy to know which coding languages that is supported on your computer, here is some code to show it:
This code will show you a message box with each language on its own line. You can replace "vb" in Creating a Compiler with one of these languages.
To compile files to an assembly we will use the CodeDomProvider.CompileAssemblyFromFile method.
This requires 2 parameters CompilerOptions As CompilerParameters and FileNames() As String.
We will use Options that we have set earlier in Compiler Options and some filenames that we will get with this code:
This code opens a dialog for selecting files, then it tries to compile the filenames with the specified CompilerOptions and returns the result to the Results variable. Later on we can check if there were any errors with the Results.Errors property.
I will now include a full example for compiling code.
This will demonstrate the different properties we have been through.
Also remember this project to see what you can make with the CodeDom compiler.
I have spent a lot of valuable time writing this tutorial so that other members or newbies can benefit from it.
A thanks would be appreciated.
Regards,
Skillful
I am glad to present my tutorial! :)
This tutorial will teach you to use the CodeDom Compiler in VB.NET
Before you start reading this tutorial I would recommend you to check out this program so that you know what can you do by reading this tutorial.
Its nice to be able to make programs with your own programs. You can customize it how you want and it dont need to have that big file size like VB.NET Express or Visual Studio. So im going to teach you how to:
- What is CodeDom?
- Importing the necessary Namespace and declaring the required Variables
- Creating a Compiler
- Compiler Options
- Adding References
- Adding Resources
- Show installed Programming Languages
- Compiling to an Assembly
- Example
- Conclusion
So lets start! :D
What is CodeDom?
CodeDom is Code Document Object Model. This provides a graphical (in the sense of graph nodes, not of a picture) representation of code in the form of a tree. This represents the hierarchy of nested code elements (namespaces contain classes, classes contain methods, methods contain variable declarations, and so on). You can use CodeDom for generating an in memory code tree, or you can use it to compile ordinary code written in a file (like we will do later). View MSDN to read all about CodeDom elements. With this code tree, you can represent any piece of code independently of a specific language.
Importing the necessary Namespace and declaring the required Variables
To begin with...
Import this namespace at the top of your code:
Code
Imports System.CodeDom.CompilerInside your class, you need to declare these variables:
Code
Private provider As CodeDomProvider
Private Options As New CompilerOptionsIn this tutorial we will be using Options as CompilerParameters and Provider as a CodeDomProvider.
Creating a Compiler
To create a compiler, you must choose what language that shall be used, for example VB, CS, CPP, etc.
To do this, use this code:
Code
If CodeDomProvider.IsDefinedLanguage("vb") Then
provider = CodeDomProvider.CreateProvider("vb")
'We will use this provider in our code later
Else
MsgBox("The specified language is not defined for this system.")
End IfOf course you can change "vb" to whatever coding language you want, but we will come back to that later in Show installed Programming Languages.
Compiler Options
We will use the class CompilerParameters to set the options for our compiler.
The available options are:
- CompilerOptions - Paramaters for compiling, for example "/optimize".
- EmbeddedResources - The .NET Resource files for embedding when compiling.
- GenerateExecutable - If true, generates an executable assembly(EXE), not a library(DLL).
- GenerateInMemory - If true, generates the compiled output in memory.
- IncludeDebugInformation - Specifies whether to create debug information for the executable file.
- LinkedResources - The .NET Resource files that is referenced in the source.
- MainClass - The main, or startup class.
- OutputAssembly - The output file.
- ReferencedAssemblies - Assemblies referenced by the current project, for example "System.dll".
- TempFiles - Temporary files for the compilation.
- TreatWarningsAsErrors - Gets or sets a value indicating whether to treat warnings as errors.
- UserToken - Gets or sets the user token to use when creating the compiler process.
- WarningLevel - Gets or sets the warning level at which the compiler aborts compilation.
- Win32Resource - Gets or sets the file name of a Win32 resource file to link into the compiled assembly.
Adding References
To add references to your project, use the ReferencedAssemblies property.
This example demonstrates how to use this property:
Code
Dim OpenReferences As New OpenFileDialog
OpenReferences.Title="Add References"
OpenReferences.MultiSelect=True
If OpenReferences.ShowDialog() = DialogResult.OK Then
Options.ReferencedAssemblies.AddRange(OpenReferences.FileNames) 'Adds a range of files selected in the file dialog
End IfIts smart to always add the System.dll reference because i cant think of any program that don't uses that. Mscorlib.dll is also a smart reference to add. To do so use this:
Code
Options.ReferencedAssemblies.Add("System.dll")
Options.ReferencedAssemblies.Add("mscorlib.dll")Adding Resources
Resources is a group of files compiled into a .NET Resource file. The can be images, sounds, text, or files. They are used by the program in different ways. One way is to display an image in a picture box, another is to change the language of the application. The EmbeddedResources property is mostly files or objects used in the application, while the LinkedResources can be culture options, these will be included in a separate assembly. To check if the assembly supports resources, use this piece of code:
Code
If Provider.Supports(GeneratorSupport.Resources) Then
'Add resource files here
End IfTo add a resource file, or more use this:
Code
Dim OpenResources As New OpenFileDialog
OpenResources.Title="Add Resources"
OpenResources.MultiSelect=True
OpenResources.Filter = "Resource files(*.resource)|*.resource"
If OpenResources.ShowDialog() = DialogResult.OK Then
Options.EmbeddedResources.AddRange(OpenResources.FileNames) 'Adds a range of files selected in the file dialog
End IfYou can change 'EmbeddedResources' to 'LinkedResources' if you want the resources to be linked in another assembly.
Show installed Programming Languages
Its handy to know which coding languages that is supported on your computer, here is some code to show it:
Code
Dim Languages As String = ""
For Each language As CompilerInfo In CodeDomProvider.GetAllCompilerInfo
For Each codingLanguage In language.GetLanguages()
Languages &= codingLanguage & vbNewLine
Next
Next
MsgBox(Languages)This code will show you a message box with each language on its own line. You can replace "vb" in Creating a Compiler with one of these languages.
Compiling to an Assembly
To compile files to an assembly we will use the CodeDomProvider.CompileAssemblyFromFile method.
This requires 2 parameters CompilerOptions As CompilerParameters and FileNames() As String.
We will use Options that we have set earlier in Compiler Options and some filenames that we will get with this code:
Code
Dim SourceFiles As New OpenFileDialog
SourceFiles.Title="Add Source Files"
SourceFiles.MultiSelect=True
Dim Results As CompilerResults
If SourceFiles.ShowDialog() = DialogResult.OK Then
Results = Provider.CompileAssemblyFromFile(Options, SourceFiles.FileNames)
End IfThis code opens a dialog for selecting files, then it tries to compile the filenames with the specified CompilerOptions and returns the result to the Results variable. Later on we can check if there were any errors with the Results.Errors property.
Example
I will now include a full example for compiling code.
This will demonstrate the different properties we have been through.
Also remember this project to see what you can make with the CodeDom compiler.
Conclusion
I have spent a lot of valuable time writing this tutorial so that other members or newbies can benefit from it.
A thanks would be appreciated.
Regards,
Skillful




![[Image: kixHQQg.jpg]](https://i.imgur.com/kixHQQg.jpg)
![[Image: 1riOqZa.png]](https://i.imgur.com/1riOqZa.png)
![[Image: 86251a37e635a21aea7eb7e014b0d820.gif]](https://i.gyazo.com/86251a37e635a21aea7eb7e014b0d820.gif)