In C#, we can create classes at run-time and compile it to generate an assembly using System.CodeDom namespace in System.dll. For this, It provides different classes and functions to create the dynamic namespace add classes, methods and properties to it.
Let's start by adding a new Console application and add the namespace using System.CodeDom to it. We will create a namespace say 'sampleNamespace' by using the CodeNamespace class. So our code will look like the following:
- CodeNamespace nameSpace = new CodeNamespace("sampleNameSpace");
Next, we will add the using's to be used in the dynamic code. This is similar to adding the namespaces which we add normally in our applications as: using System or using System.Linq. To do this, we will use the Imports function in the CodeNamespace class. So our code will look like the following:
- nameSpace.Imports.Add(new CodeNamespaceImport("System"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
Next we will add a class named SampleClass to our namespace sampleNamespace using the CodeTypeDeclaration class. It provides different properties like Name, IsClass, Attributes etc. to create class definition and add this class to our namespace sampleNamespace. So the code will look like the following:
- CodeTypeDeclaration cls = new CodeTypeDeclaration();
- cls.Name = "SampleClass";
- cls.IsClass = true;
- cls.Attributes = MemberAttributes.Public;
- nameSpace.Types.Add(cls);
Next we will use the CodeCompileUnit class, which will acts as a container for the dynamic code that we are trying to generate. This container will be used to compile and generate an assembly of our code. Let's create a physical file on our system using the CodeCompileUnit class using the StreamWriiter class. So our code will change to the following:
- CodeCompileUnit compileUnit = new CodeCompileUnit();
- compileUnit.Namespaces.Add(nameSpace);
- CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();
- CSharpCodeProvider provider = new CSharpCodeProvider();
- using (StreamWriter sw = new StreamWriter(@"C:\SampleFile.cs", false))
- {
- IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
- provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
- tw.Close();
- }
So our complete code will look like below:
- CodeNamespace nameSpace = new CodeNamespace("sampleNameSpace");
- nameSpace.Imports.Add(new CodeNamespaceImport("System"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
- nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
- CodeTypeDeclaration cls = new CodeTypeDeclaration();
- cls.Name = "SampleClass";
- cls.IsClass = true;
- cls.Attributes = MemberAttributes.Public;
- nameSpace.Types.Add(cls);
- CodeCompileUnit compileUnit = new CodeCompileUnit();
- compileUnit.Namespaces.Add(nameSpace);
- CSharpCodeProvider csharpcodeprovider = new CSharpCodeProvider();
- CSharpCodeProvider provider = new CSharpCodeProvider();
- using (StreamWriter sw = new StreamWriter(@"D:\TestFile.cs", false))
- {
- IndentedTextWriter tw = new IndentedTextWriter(sw, " ");
- provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions());
- tw.Close();
- }
- //------------------------------------------------------------------------------
- // <auto-generated>
- // This code was generated by a tool.
- // Runtime Version:4.0.30319.42000
- //
- // Changes to this file may cause incorrect behavior and will be lost if
- // the code is regenerated.
- // </auto-generated>
- //------------------------------------------------------------------------------
- namespace sampleNameSpace {
- using System;
- using System.Linq;
- using System.Text;
- public class SampleClass {
- }
- }
So this was about how we can generate dynamic class with namespace using CodeDom classes. In next article we will see how we can add methods to these classes. Hope you enjoyed reading it. Happy coding...!!!
love it.
ReplyDelete