Mohammed Atef’s Technical blog

Generate and Compile Csharp files using CodeDOM

Hello, I was trying to create .net utility to merge two CSharp files or more into one CSharp file, while working into this task I had use CodeDOM, really it’s very useful .So, I want to give you simple brief about CodeDOM.


The CodeDOM provides types that represent many common types of source code elements. You can design a program that builds a source code model using CodeDOM . The CodeDOM can also be used to compile source code into a binary assembly.

Common uses

1- Template code generation: generating code for ASP.NET, XML Web services client proxies, code wizards, designers, or other code-emitting mechanisms.
2- Dynamic compilation: supporting code compilation in single or multiple languages.

After understanding CodeDOM let’s learn how to use CodeDOM to generate CSharp class and compile the generated source code into assembly.
It’s very simple if you are programmer and you want to write class you will follow steps.
First step
Create NameSpace and and import class NameSpaces.




CodeNamespace namespaceObj=new CodeNamespace();


namespaceObj.Imports.Add(new CodeNamespaceImport(“System”));


namespaceObj.Imports.Add(new CodeNamespaceImport(“System.Xml”));

Second step
Create Class or Type.




CodeTypeDeclaration clsobj = new CodeTypeDeclaration();


clsobj.Name = “Classname”;


clsobj.IsClass = true;           


clsobj.IsEnum = false;


clsobj.TypeAttributes = TypeAttributes.Public|TypeAttributes.Serializable; 



Third Step

Create property and member




CodeTypeReference teftype = new CodeTypeReference(System.String.ToString());


CodeMemberField memberfield = new CodeMemberField();


memberfield.Type = teftype;


memberfield.Name = “firstname”;


memberfield.Attributes = MemberAttributes.Public;



//create property




CodeMemberProperty property = new CodeMemberProperty();


property.Name = “Firstname”;


CodeTypeReference teftype = new CodeTypeReference(System.String.ToString());


property.Type = teftype;                       


property.Attributes = MemberAttributes.Public;           




CodeSnippetExpression getsnippet = new CodeSnippetExpression(“return firstname”);


CodeSnippetExpression setsnippet = new CodeSnippetExpression(“firstname=value”);





Last Step
Compile code and create new assembly





CodeCompileUnit assembly = new CodeCompileUnit();




CSharpCodeProvider prov = new CSharpCodeProvider();


ICodeCompiler icc = prov.CreateCompiler();    


CompilerParameters options = new CompilerParameters();


options.GenerateInMemory = true;







options.OutputAssembly = “c:\\Temp\Sample.dll”;


options.GenerateExecutable = false;


options.IncludeDebugInformation = false;


CompilerResults compres = icc.CompileAssemblyFromDom(options, assembly); 


The previous code used when you need to generate new CSharp class and compile it.
But sometimes you need to compile existing CSharp class!!
Don’t worry you can do this task using this few line of code





CodeNamespace namespaceObj=new CodeNamespace();


namespaceObj.Imports.Add(new CodeNamespaceImport(“System”));


namespaceObj.Imports.Add(new CodeNamespaceImport(“System.Xml”));


Stream codeFile = File.Open(“c:\\Temp\Sample.cs”,FileMode.Create);


StreamWriter sw = new StreamWriter(codeFile);


CSharpCodeProvider cscp = new CSharpCodeProvider();


ICodeGenerator codeGenerator = cscp.CreateGenerator(sw);


CodeGeneratorOptions cgo = new CodeGeneratorOptions();


codeGenerator.GenerateCodeFromNamespace(namespaceobj, sw, cgo);           







The System.CodeDom namespace in .net allows the users to dynamically compile and create assemblies, I hope this article is helpful for you.




February 19, 2009 Posted by | C#,VB.Net, Developement | , | Leave a comment