Introduction
The System.CodeDom namespace in .net allows the users to dynamically compile and create assemblies. This concept is used by ASP.NET internally. The article provides an insight on how to create assemblies dynamically.
The following are the list of classes, which would be used to create the assembly:
- CodeNamespace
- CodeTypeDeclaration
- CodeCompileUnit
- CodeEntryPointMethod
- CodeMemberField
- CodeMemberProperty
- CodeMemberMethod
- CodeParameterDeclarationExpression
- CodeCompileUnit
- CompilerParameters
- CodeSnippetExpression
- CodeExpressionStatement
Step 1. Add the following namespace declarations.
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
The System.The codedom namespace contains the classes/types necessary to create assemblies at runtime. The above-mentioned classes are contained within this namespace.
Step 2. Create the following class.
public class CCodeGenerator {
CodeNamespace mynamespace;
CodeTypeDeclaration myclass;
CodeCompileUnit myassembly;
}
Any code created from Step 3 to Step 10 must reside in the CcodeGenerator class. For clarity and explanation sake, the article has fragmented the code into pieces.
Step 3. The CodeNamespace class is used to add/create namespaces. The name of the namespace can be passed through the constructor of the class.
public void CreateNamespace()
{
mynamespace = new CodeNamespace("mynamespace");
}
Step 4. Using the mynamespace object, add the list of all namespaces that are used by the assembly.
public void CreateImports()
{
mynamespace.Imports.Add(new CodeNamespaceImport("System"));
mynamespace.Imports.Add(new CodeNamespaceImport("System.Drawing"));
mynamespace.Imports.Add(new CodeNamespaceImport("System.Windows.Forms"));
}
Step 5. Create a class inside the namespace.
public void CreateClass()
{
myclass = new CodeTypeDeclaration();
// Assign a name for the class
myclass.Name = "CMyclass";
myclass.IsClass = true;
// Set the Access modifier.
myclass.Attributes = MemberAttributes.Public;
// Add the newly created class to the namespace
mynamespace.Types.Add(myclass);
}
Step 6. Create a member and add it to the newly created class.
public void CreateMember() {
// Provide the type and variable name
CodeMemberField mymemberfield = new CodeMemberField(typeof(System.String), "strMessage");
// Add the member to the class
myclass.Members.Add(mymemberfield);
}
Step 7. Create a property (with get and set) and add it to the class.
public void CreateProperty() {
CodeMemberProperty mymemberproperty = new CodeMemberProperty();
// Name of the property
mymemberproperty.Name = "Message";
// Data type of the property
mymemberproperty.Type = new CodeTypeReference(typeof(System.String));
// Access modifier of the property
mymemberproperty.Attributes = MemberAttributes.Public;
// Add the property to the class
myclass.Members.Add(mymemberproperty);
// Add the code-snippets to the property.
// If required, we can also add some custom validation code.
// using the CodeSnippetExpression class.
// Provide the return <propertyvalue> statement-For getter
CodeSnippetExpression getsnippet = new CodeSnippetExpression("return strMessage");
// Assign the new value to the property-For setter
CodeSnippetExpression setsnippet = new CodeSnippetExpression("strMessage=value");
// Add the code snippets into the property
mymemberproperty.GetStatements.Add(getsnippet);
mymemberproperty.SetStatements.Add(setsnippet);
}
Step 8. Create a method. Please take utmost care when assigning values to the CodeSnippetExpression class, as any statement provided must not have any syntax error(s). Since the assembly would be created at runtime (dynamically), the compiler would simply throw all Compile-time error(s) and we don't have any prudent means to find and eliminate the error.
public void CreateMethod() {
// Create an object of the CodeMemberMethod
CodeMemberMethod mymethod = new CodeMemberMethod();
// Assign a name for the method.
mymethod.Name = "AddNumbers";
// Create two parameters
CodeParameterDeclarationExpression cpd1 = new CodeParameterDeclarationExpression(typeof(int), "a");
CodeParameterDeclarationExpression cpd2 = new CodeParameterDeclarationExpression(typeof(int), "b");
// Add the parameters to the method.
mymethod.Parameters.AddRange(new CodeParameterDeclarationExpression[] { cpd1, cpd2 });
// Provide the return type for the method.
CodeTypeReference ctr = new CodeTypeReference(typeof(System.Int32));
// Assign the return type to the method.
mymethod.ReturnType = ctr;
// Provide definition to the method (returns the sum of two numbers)
CodeSnippetExpression snippet1 = new CodeSnippetExpression("System.Console.WriteLine(\" Adding :\" + a + \" And \" + b )");
// Return the value
CodeSnippetExpression snippet2 = new CodeSnippetExpression("return a+b");
// Convert the snippets into Expression statements.
CodeExpressionStatement stmt1 = new CodeExpressionStatement(snippet1);
CodeExpressionStatement stmt2 = new CodeExpressionStatement(snippet2);
// Add the expression statements to the method.
mymethod.Statements.Add(stmt1);
mymethod.Statements.Add(stmt2);
// Provide the access modifier for the method.
mymethod.Attributes = MemberAttributes.Public;
// Finally add the method to the class.
myclass.Members.Add(mymethod);
}
Step 9. Create a main method (Entry point for the assembly).
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 definition 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)");
// Invoke 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.