none
How to compile C# source code from a string? RRS feed

  • Question

  • Hi,

    How can I convert a string (e.g. loaded from a .txt file) into
    an ingredient of the C# code?

    e.g.: string = "Console.WriteLine("Welcome");"

    How can I implenent this string into a method,
    How to compile C# source code from this string?

    Thanks,
    Tom
    Sunday, April 4, 2010 12:03 PM

Answers

  • Check out this article

    http://support.microsoft.com/kb/304655

     


    Miha Markic [MVP C#] http://blog.rthand.com
    Sunday, April 4, 2010 12:52 PM
  • Hi,

    It is possible with CSharpCodeProvider Class.

                CSharpCodeProvider provider = new CSharpCodeProvider();
                ICodeCompiler compiler = provider.CreateCompiler();
                CompilerParameters parameters = new CompilerParameters();
                parameters.GenerateExecutable = true;
                parameters.OutputAssembly = "bin.exe";
                string code = "using System; namespace HelloWorld { class Prog { static void Main(string[] args){Console.WriteLine(\"Hello World!\");Console.ReadLine();}}}";
                CompilerResults cr = compiler.CompileAssemblyFromSource(parameters, code);
    
                if (cr.Errors.Count > 0)
                {
                    Console.WriteLine("Errors building into {0}",
                         cr.PathToAssembly);
                    foreach (var ce in cr.Errors)
                    {
                        Console.WriteLine("{0}", ce.ToString());
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("Source built into {0} successfully.",
                         cr.PathToAssembly);
                    // Launch
                    Process.Start("bin.exe");
                }

    Regards,

    • Proposed as answer by Louis.fr Monday, April 5, 2010 1:23 AM
    • Marked as answer by YiChun Chen Tuesday, April 6, 2010 10:56 AM
    Sunday, April 4, 2010 3:40 PM

All replies

  • Hi,

    I think is not possible unless your string have complete C# code with Main method under a class.

    After u have complete code, just compile it with

    csc /out:Test.exe Test.cs
    Sunday, April 4, 2010 12:27 PM
  • Check out this article

    http://support.microsoft.com/kb/304655

     


    Miha Markic [MVP C#] http://blog.rthand.com
    Sunday, April 4, 2010 12:52 PM
  • Hi,

    It is possible with CSharpCodeProvider Class.

                CSharpCodeProvider provider = new CSharpCodeProvider();
                ICodeCompiler compiler = provider.CreateCompiler();
                CompilerParameters parameters = new CompilerParameters();
                parameters.GenerateExecutable = true;
                parameters.OutputAssembly = "bin.exe";
                string code = "using System; namespace HelloWorld { class Prog { static void Main(string[] args){Console.WriteLine(\"Hello World!\");Console.ReadLine();}}}";
                CompilerResults cr = compiler.CompileAssemblyFromSource(parameters, code);
    
                if (cr.Errors.Count > 0)
                {
                    Console.WriteLine("Errors building into {0}",
                         cr.PathToAssembly);
                    foreach (var ce in cr.Errors)
                    {
                        Console.WriteLine("{0}", ce.ToString());
                        Console.WriteLine();
                    }
                }
                else
                {
                    Console.WriteLine("Source built into {0} successfully.",
                         cr.PathToAssembly);
                    // Launch
                    Process.Start("bin.exe");
                }

    Regards,

    • Proposed as answer by Louis.fr Monday, April 5, 2010 1:23 AM
    • Marked as answer by YiChun Chen Tuesday, April 6, 2010 10:56 AM
    Sunday, April 4, 2010 3:40 PM