Dynamic Class Using C#

In this article, we will see how we can create C# class at run-time and compile it to generate an assembly. System.CodeDom namespace in System.dll provides different classes and functions, using which we can create the dynamic namespace, add classes to it, and then add methods and properties to it.
 
To start with, we will add a new Console Application and add the namespace, using System.CodeDOM to it. First, we will create a namespace of our choice, say 'myNamespace'. For this, we will use the CodeNamespace class. Thus, our code will look like:
  1. CodeNamespace nameSpace = new CodeNamespace("myNameSpace");  
Next, we will add our list to be used in the dynamic code and we are going to generate it. This is nothing but dynamically adding the namespaces which we add normally in our Applications such as: using System or using System.Linq. For this, we will use the Imports function in the CodeNamespace class. Thus, our code will look like:
  1. nameSpace.Imports.Add(new CodeNamespaceImport("System"));  
  2. nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));  
  3. nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));  
Next, we will add a class named MyClass to our namespace myNamespace, using the CodeTypeDeclaration class. This class will provide different properties like Name, IsClass, Attributes etc., which are used to configure our class definition. Thus, this class will be added to our namespace myNamespace. Hence, the code will look like:
  1. CodeTypeDeclaration cls = new CodeTypeDeclaration();  
  2. cls.Name = "MyClass";  
  3. cls.IsClass = true;  
  4. cls.Attributes = MemberAttributes.Public;  
  5. nameSpace.Types.Add(cls);  
Next, we will use the CodeCompileUnit class. This acts as a container, which will be used as a container for the entire code we have written to compile and generate an assembly for it. To see, how the code looks, let's create a physical fill on our system, using the CodeCompileUnit class. For this, we will use StreamWriter class. Thus, our code will change to the following:  
  1. CodeCompileUnit compileUnit = new CodeCompileUnit();  
  2. compileUnit.Namespaces.Add(nameSpace);  
  3. CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();   
  4.   
  5. CSharpCodeProvider provider = new CSharpCodeProvider();  
  6.   
  7. using (StreamWriter sw = new StreamWriter(@"D:\TestFile.cs"false))  
  8. {  
  9.     IndentedTextWriter tw = new IndentedTextWriter(sw, "    ");  
  10.     provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());  
  11.     tw.Close();  
  12. }  
The complete code will look like:
  1.           CodeNamespace nameSpace = new CodeNamespace("myNameSpace");  
  2.   
  3.           nameSpace.Imports.Add(new CodeNamespaceImport("System"));  
  4.           nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));  
  5.           nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));  
  6.   
  7.           CodeTypeDeclaration cls = new CodeTypeDeclaration();  
  8.           cls.Name = "MyClass";  
  9.           cls.IsClass = true;  
  10.           cls.Attributes = MemberAttributes.Public;  
  11.           nameSpace.Types.Add(cls);  
  12.   
  13.           CodeCompileUnit compileUnit = new CodeCompileUnit();  
  14.           compileUnit.Namespaces.Add(nameSpace);  
  15.           CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();   
  16.   
  17.           CSharpCodeProvider provider = new CSharpCodeProvider();  
  18.   
  19.           using (StreamWriter sw = new StreamWriter(@"D:\TestFile.cs"false))  
  20.           {  
  21.               IndentedTextWriter tw = new IndentedTextWriter(sw, "    ");  
  22.               provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());  
  23.               tw.Close();  
  24.           }  
Run the code and open the location to see the code file, which got generated with your code:
  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. //     This code was generated by a tool.  
  4. //     Runtime Version:4.0.30319.42000  
  5. //  
  6. //     Changes to this file may cause incorrect behavior and will be lost if  
  7. //     the code is regenerated.  
  8. // </auto-generated>  
  9. //------------------------------------------------------------------------------  
  10.   
  11. namespace myNameSpace {  
  12.     using System;  
  13.     using System.Linq;  
  14.     using System.Text;  
  15.       
  16.       
  17.     public class MyClass {  
  18.     }  
  19. }  
Hence, this was about how we can generate the class with the namespace, using CodeDOM classes. In the next article, we will see how we can add the methods to these classes. Hope you enjoyed reading it. Happy coding...!!! 


Similar Articles