Answered by:
How to compile C# source code from a string?

Question
-
Answers
-
Check out this article
http://support.microsoft.com/kb/304655
Miha Markic [MVP C#] http://blog.rthand.com- Proposed as answer by Yasser Zamani - Mr. Help Sunday, April 4, 2010 3:15 PM
- Marked as answer by YiChun Chen Tuesday, April 6, 2010 10:56 AM
-
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
All replies
-
-
Check out this article
http://support.microsoft.com/kb/304655
Miha Markic [MVP C#] http://blog.rthand.com- Proposed as answer by Yasser Zamani - Mr. Help Sunday, April 4, 2010 3:15 PM
- Marked as answer by YiChun Chen Tuesday, April 6, 2010 10:56 AM
-
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