A simplest/dumbest CSharp IDE

By admin - Last updated: Sunday, November 29, 2009 - Save & Share - Leave a Comment

The tiny WPF app (14k) has a textbox allows the input of the C# code, it compiles the code and execute the code after press F5 key.
Here is what it looks like. No surprise.

codef5

The main part which compiles the code at runtime is using CSharpCodeProvider from Microsoft.CSharp namespace.

static CompilerResults Build(string code, IEnumerable<string> references)
{
    // set compiler version
    var options = new Dictionary<string, string>
    {
        { “CompilerVersion”, “v3.5″ }
    };

    // init compiler
    CodeDomProvider compiler = new CSharpCodeProvider(options);

    // set the compilation params
    var parameters = new CompilerParameters
    {
        WarningLevel = 4,
        TreatWarningsAsErrors = false,
        IncludeDebugInformation = true,
        GenerateExecutable = true,
        GenerateInMemory = false,
        OutputAssembly = OutputAssemblyPath
    };

    // add referenced assemblies if any
    if (references != null)
    {
        foreach (var path in references)
        {
            parameters.ReferencedAssemblies.Add(path);
        }
    }

    // compiles and returnt the result
    return compiler.CompileAssemblyFromSource(parameters, code);
}

At runtime, it gets translates to perform the following task (path is used as in my environment)

D:\src\CodeF5\bin\Debug> “C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe” /t:exe /utf8output /R:”System.dll” /R:”System.Core.dll” /R:”System.Xml.dll” /R:”System.Xml.Linq.dll” /out:”D:\dev\trunk\proj\CodeF5\src\CodeF5\bin\Debug\MyOutput\My.exe” /D:DEBUG /debug+ /optimize- /w:4 “C:\Users\admin\AppData\Local\Temp\po4a4o8n.0.cs”

Download the source code




Posted in Random • Tags: , Top Of Page

Write a comment


Captcha: 6 − five =