public void CreateEntryPoint()
{
//Create an object and assign the name as "Main"
CodeEntryPointMethod mymain = new CodeEntryPointMethod();
mymain.Name = "Main";
//Mark the access modifier for the main method as Public and //static
mymain.Attributes = MemberAttributes.Public | MemberAttributes.Static;
//Provide defenition to the main method.
//Create an object of the "Cmyclass" and invoke the method
//by passing the required parameters.
CodeSnippetExpression exp1 = new CodeSnippetExpression("CMyclass x = new CMyclass()");
//Assign value to our property
CodeSnippetExpression exp2 = new CodeSnippetExpression("x.Message=\"Hello World \"");
//Print the value in the property
CodeSnippetExpression exp3 = new CodeSnippetExpression("Console.WriteLine(x.Message)");
//Invode the method
CodeSnippetExpression exp4 = new CodeSnippetExpression("Console.WriteLine(\"Answer: {0}\",x.AddNumbers(10,20))");
CodeSnippetExpression exp5 = new CodeSnippetExpression("Console.ReadLine()");
//Create expression statements for the snippets
CodeExpressionStatement ces1 = new CodeExpressionStatement(exp1);
CodeExpressionStatement ces2 = new CodeExpressionStatement(exp2);
CodeExpressionStatement ces3 = new CodeExpressionStatement(exp3);
CodeExpressionStatement ces4 = new CodeExpressionStatement(exp4);
CodeExpressionStatement ces5 = new CodeExpressionStatement(exp5);
//Add the expression statements to the main method.
mymain.Statements.Add(ces1);
mymain.Statements.Add(ces2);
mymain.Statements.Add(ces3);
mymain.Statements.Add(ces4);
mymain.Statements.Add(ces5);
//Add the main method to the class
myclass.Members.Add(mymain);
}
Step 10: Compile the class and create the assembly.
public void SaveAssembly()
{
//Create a new object of the global CodeCompileUnit class.
myassembly = new CodeCompileUnit();
//Add the namespace to the assembly.
myassembly.Namespaces.Add(mynamespace);
//Add the following compiler parameters. (The references to the //standard .net dll(s) and framework library).
CompilerParameters comparam = new CompilerParameters(new string[] {"mscorlib.dll"});
comparam.ReferencedAssemblies.Add("System.dll");
comparam.ReferencedAssemblies.Add("System.Drawing.dll"); comparam.ReferencedAssemblies.Add("System.Windows.Forms.dll");
//Indicates Whether the compiler has to generate the output in //memory
comparam.GenerateInMemory = false;
//Indicates whether the output is an executable.
comparam.GenerateExecutable = true;
//provide the name of the class which contains the Main Entry //point method
comparam.MainClass = "mynamespace.CMyclass";
//provide the path where the generated assembly would be placed
comparam.OutputAssembly = @"c:\temp\HelloWorld.exe";
//Create an instance of the c# compiler and pass the assembly to //compile
Microsoft.CSharp.CSharpCodeProvider ccp = new Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler icc = ccp.CreateCompiler();
//The CompileAssemblyFromDom would either return the list of
//compile time errors (if any), or would create the
//assembly in the respective path in case of successful //compilation
CompilerResults compres = icc.CompileAssemblyFromDom(comparam,myassembly);
if (compres == null || compres.Errors.Count>0)
{
for (int i=0; i<compres.Errors.Count;i++)
{
Console.WriteLine(compres.Errors[i]);
}
}
}
Step 11: Create a test component to create the assembly.
using System;
namespace DynamicAssemblies
{
class testCodeDom
{
[STAThread]
static void Main(string[] args)
{
CCodeGenerator cg = new CCodeGenerator();
cg.CreateNamespace();
cg.CreateImports();
cg.CreateClass();
cg.CreateMember();
cg.CreateProperty();
cg.CreateMethod();
cg.CreateEntryPoint();
cg.SaveAssembly();
Console.ReadLine();
}
}
}
The HelloWorld.exe would be created in the mentioned path. The output of the assembly would be as follows:

Conclusion
The facility to dynamically create assemblies would be of immense use, when developing real-world applications that demand a great amount of flexibility and scalability.