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.

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; 

 

namespaceobj.Types.Add(cls);

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;

 

clsobj.Members.Add(memberfield);

//create property

 

 

 

CodeMemberProperty property = new CodeMemberProperty();

 

property.Name = “Firstname”;

 

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

 

property.Type = teftype;                       

 

property.Attributes = MemberAttributes.Public;           

 

clsobj.Members.Add(property);

 

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

 

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

 

property.GetStatements.Add(getsnippet);

 

property.SetStatements.Add(setsnippet);

Last Step
Compile code and create new assembly

 

 

 

 

CodeCompileUnit assembly = new CodeCompileUnit();

 

assembly.Namespaces.Add(namespaceobj);

 

CSharpCodeProvider prov = new CSharpCodeProvider();

 

ICodeCompiler icc = prov.CreateCompiler();    

 

CompilerParameters options = new CompilerParameters();

 

options.GenerateInMemory = true;

 

options.ReferencedAssemblies.Add(“System.Xml.dll”);

 

options.ReferencedAssemblies.Add(“System.dll”);                   

 

 

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);           

 

sw.Close();

 

codeFile.Close();

 

 

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

 

 

Advertisements

February 19, 2009 - Posted by | C#,VB.Net, Developement | ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: