Search code examples
c#compilationgeneric-programming

how to create a program that generate a source code programatically and compile it and execute after successfull compilation


How to create a program (Parent Program) that creates .cs(generate source code programatically from a Parent Program) file and compile it and execute after successfull compilation and communicate result with Parent program.

What i done is

Step 1: i can create file using c# application using System.IO namespace

Step 2: there is option that we can compile program by command line using "csc" from console can execute it by using Prosess.Start() in System.Globalization namespace

But the Problems are

  1. how to know it's compiled properly.

  2. how to communicate Parent program with generated program


Solution

  • Take a look at System.Reflection.Emit - that allows you to generate MSIL assemblies at runtime. Here are two starting points:

    http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx

    http://msdn.microsoft.com/en-us/library/3y322t50.aspx