Translate

Monday, February 13, 2012

System.CodeDom.Compiler Namespace ( with example)

System.CodeDom.Compiler:  This namespace lets you generate source code files on the fly (at runtime). It also lets you generate assemblies (dlls) from these generated source code at runtime.


Note that it can only generate code in one of the four managed languages (C#, vb, Jscript, C++)


Given below is a simple example that uses CodeDomProvider class in the CodeDom.Compiler namespace.

using System;
using System.CodeDom.Compiler;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            CodeDomProvider provider;
            string language = "c#";

            provider = CodeDomProvider.CreateProvider(language);


            Console.WriteLine(provider.ToString());//Displays Microsoft.CSharp.CSharpCodeProvider
            Console.WriteLine(provider.FileExtension);//Displays cs
        }
    }
}



If you add a watch on CodeDomProvider.GetAllCompilerInfo()  this is what you see
















No comments:

Post a Comment

Comments will appear once they have been approved by the moderator