Dynamic Building and Execution of Assembly Using CodeDome & Reflection

We have an assembly running, now at run time we want to enter some code, compile it, execute it, and apart from that access code and data member from the main or other referenced assembly. 
Please Download the Source code (that will help a lot to understand you)
Before we start our game know few rules:
  1. System has an executing assembly named Pradeep.exe which contains two classes 
    public static  Program  :- contains static void Main() and   
    1. static int StaticFunctionInProgram(int value) 
    public class NewProgram :- contains public int InstanceFunctionInProgram(int value)
    Why we need these two classes? If u look carefully you can answer simple concept of OOPs that forced to have two classes. 
  2. Now my aim is that we will write code (more specifically function Body) that will be compiled then executed and resulting string will be shown into second richtextBox in form.
  3. What is the purpose of the other controls in form, you know.
  4. this Demo is prepared in .net 2.0, you can change it to .net 1.x varsion.
Lets Start:
1. Place some code into 1st textBox  (ignore {} brackets, just place code), click Run button, it will call  button1_Click which will call MyDynamicAssemblyCreateFunction() and then.
2. [string startConstantPart = "using System; \n class DyanamicAsemblyClass \n { \n \r public static string MyFunction() \n{ " ; ] will declare a string that contains a class declaration and a static function inside that class, But as STRING; same is [string endConstantPart = " return \"Default return value \" ; \n\n}\n\n}";] end part.
3. Body will come from Forms rftFunctionBody box. And this will finally prepare a string named : completeString, as name suggest it is complete string means functionbody inside a function inside a class.
4. Easy part is done, now look easer part
5. Compile it and execute it that all, but how?
6. Answer is CodeDome + Reflection.
7. Prepare a Dome (using CodeDome and CodeDome.Compilor) as its name suggest then invoke (using Reflection) that dynamically compiled function and that's it.
8. Take 1st part, Prepare a CodeDome, we have String which we want to compile and this [CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");] will provide a c# provider to create a CodeDome.
9. Now we need to set few options like I want a DLL instead of EXE when code compile, that DLL must be loaded into Memory, treat warning as error, like that there are n number of options that you can set but for that you need a object store all these information and that devil is [CompilerParameters parameters = new CompilerParameters();] here.
10. Sorry I forgot to mention its not devil, we have to add references to this only like [parameters.ReferencedAssemblies.Add("System.dll");]
11. Now doing all this stupid thing must produce some result and that is the final nectar we are looking for and here's the nectar [CompilerResults results = provider.CompileAssemblyFromSource(parameters, completeString);] a function [CompileAssemblyFromSource] of [CodeDomeProvider] is used to produce result we have some siblings of this function- very useful.
12. [results.Errors.HasErrors] test of the nectar for the purity.
13. And finally codeDome end his job. Its time to say hello to Reflection.
14. [Assembly assm = results.CompiledAssembly;] take the reference of the dynamically build assembly. This assembly contains every thing as entity which was as string in [completeString].
15. [Type ClassType = assm.GetType("DyanamicAsemblyClass");] we need class Type to get the information about function.
16. [MethodInfo methodInfo = ClassType.GetMethod("MyFunction");] information about method, which we are supposed to invoke. Remember this method is Public and Static. Why ? Because, I am going to info that method instance of the class. Wait you can create an instance of the class and invoke member functions(Try it your self).
17. [object temp = methodInfo.Invoke(null, null);] finally invoked, this function returns  object Type value. And overloaded function, but this is  Invoke("object on which to invoke method null if static method "," argument list null in no argument is supplied").
18. Finally a string value is returned that is shown in the Second text box.
19. Now easiest part if you add the reference of the Pradeep.dll in compiler object u can use types defined into your own assembly.
20. Try this out, in your code create object of  public class NewProgram. Then only you will know why it is public class and why I need this class even though I was having a static class Program( A static class can't have non static entities).
Pradeep http://tiwaripradeep.blogspot.com/