In the previous tutorial we have explored the very basics of IL programming and now we will complete our understanding the basics. The following topics will be covered:
- Local variables
- Boxing and unboxing
- Ref and out parameter modifiers
Let’s get started!
Some days ago I realized that it will be 2 years now since I’ve published my article Reweaving IL code with Mono.Cecil on Codeproject and one thing I wanted to do is to write tutorials that teaches coding in the Common Intermediate Language.
Why create another tutorials about IL coding you ask? Because while there are some good ones out there, I wanted to create a series of tutorials with a practical approach to CIL programming with the learning by doing approach 🙂
In my last post I’ve shown how to create a project template from an existing project by exporting it. Now we will go through the creation of a simple VSIX to package our project template.
Steps before creating a VSIX project
- Install the Visual Studio 2013 SDK which you can download from here.
- If you have not created a project template before, read this post.
Recently at work we had to create a lot of microservices where we required to copy-paste lot of files from one sample solution to the specific service implementations. The necessity of this approach comes from the many small differences between each service.
Of course the common code was extracted to shared libraries but this was just a partial solution. Now that we can grasp their usefulness, let’s see how to create visual studio templates!
Different options for creating templates
There are three ways to create a project template:
- Exporting a template from an existing project
- Creating a template project
- Exporting an existing project then copying its content to a template project
In this tutorial we will explore the first option.
Recently I have installed the Visual Studio 2013 SDK and encountered a very annoying error for the first time. After opening a project (any project) at the Solution explorer none of the projects were loaded. And even a more annoying error occurred when I tried to close the solution, I could not do it and was prompted with an error:
No exports were found that match the constraint: ContractName
To solve this problem I found these tips:
- Uninstall one of the extensions then restart Visual Studio. I know this sounds idiotic but a lot of people wrote this helps
- Repair the latest update for Visual Studio which worked for me. (You can found Update 4 for Visual Studio 2013 here
Since the release of Entity Framework 6.0 it became possible to create custom migration operations enabling us to make operations similar to the CreateTable and DropTable functions. This way we can encapsulate more database definitions such as adding a Check constraint or a description to a column (or any other object).
Of course we had means to do this before version 6.0 as well using the Sql function of the DbMigration which looked like this:
Sql("ALTER TABLE [Products] ADD CONSTRAINT [CK_Products_SKU] " +
"CHECK (SKU LIKE '[A-Z][A-Z]-[0-9][0-9]%')");
Creating a migration operation is however a big improvement over using the Sql function in the migration step and executing the SQL statement that was passed in regardless what database is used. Thus the code we get using custom migration operations not only looks better but it is database agnostic as well.