Posts

Showing posts from August, 2022

How to Create/Generate Project File Programmatically in .Net?

Image
There are situations where the source files are generated dynamically either through templates or codeDOM, therefore we need a mechanism to package those source files in a project.   .Net allows to generate project files dynamically using the Microsoft.Build.Construction. Generating Project File Dynamically In this post lets see with an example how we can generate csproj file, add the source files and references dynamically into the csproj file. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 using Microsoft.Build.Construction ; namespace OnTheFlyCompilation { class Program { static void Main ( string [] args) { var root = ProjectRootElement.Create(); var group = root.AddPropertyGroup(); group .AddProperty( "Configuration" , "Release" ); group .AddProperty( "Platform" , "x64" ); // referen...

How to Compile Source Code in .Net Dynamically ? | Generate Assembly Dynamically

Image
  .Net Framework allows us to generate as well as compile our source code dynamically. We have already seen how we can generate the source code with the help of T4 Text Template in our previous post . Dynamic compilation is supported by .Net Framework with the help of CSharpCodeProvider class which is defined in the assembly System.CodeDom.dll. Dynamic Compilation Using CSharpCodeProvider In this post we will see how to use this class to compile our source code dynamically and generate an output assembly with an example. In our example we will create a Console Application in .Net Framework which uses CSharpCodeProvider to compile our source code using Compiler Parameters. Sample Code Defining source codes to be used for compilation:  Below code is used in our sample application as input source files which will be used for compilation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 ...