Tengo una duda acerca de C# y como podria hacer un builder.
He estado mirando acerca de compilacion en tiempo de ejecucion para poder compilar mi proyecto con las opciones que en ese momento sean oportunas y he encotnrado esto
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Compile-C-or-VB-source-code-run-time.html y esto tambien!
http://support.microsoft.com/kb/304655
Que me ha parecido super interesante!
Para el que sea un poco vaguete y no le apetezca mirar las web que he dejado aqui esta lo que hace posible la compilacion en tiempo de ejecucion en un programa:
Código
/// <summary> /// Function to compile .Net C#/VB source codes at runtime /// </summary> /// <param name="_CodeProvider">Base class for compiler provider</param> /// <param name="_SourceCode">C# or VB source code as a string</param> /// <param name="_SourceFile">External file containing C# or VB source code</param> /// <param name="_ExeFile">File path to create external executable file</param> /// <param name="_AssemblyName">File path to create external assembly file</param> /// <param name="_ResourceFiles">Required resource files to compile the code</param> /// <param name="_Errors">String variable to store any errors occurred during the process</param> /// <returns>Return TRUE if successfully compiled the code, else return FALSE</returns> private bool CompileCode(System.CodeDom.Compiler.CodeDomProvider _CodeProvider, string _SourceCode, string _SourceFile, string _ExeFile, string _AssemblyName, string[] _ResourceFiles, ref string _Errors) { // set interface for compilation System.CodeDom.Compiler.ICodeCompiler _CodeCompiler = _CodeProvider.CreateCompiler(); // Define parameters to invoke a compiler System.CodeDom.Compiler.CompilerParameters _CompilerParameters = if (_ExeFile != null) { // Set the assembly file name to generate. _CompilerParameters.OutputAssembly = _ExeFile; // Generate an executable instead of a class library. _CompilerParameters.GenerateExecutable = true; _CompilerParameters.GenerateInMemory = false; } else if (_AssemblyName != null) { // Set the assembly file name to generate. _CompilerParameters.OutputAssembly = _AssemblyName; // Generate an executable instead of a class library. _CompilerParameters.GenerateExecutable = false; _CompilerParameters.GenerateInMemory = false; } else { // Generate an executable instead of a class library. _CompilerParameters.GenerateExecutable = false; _CompilerParameters.GenerateInMemory = true; } // Generate debug information. //_CompilerParameters.IncludeDebugInformation = true; // Set the level at which the compiler // should start displaying warnings. _CompilerParameters.WarningLevel = 3; // Set whether to treat all warnings as errors. _CompilerParameters.TreatWarningsAsErrors = false; // Set compiler argument to optimize output. _CompilerParameters.CompilerOptions = "/optimize"; // Set a temporary files collection. // The TempFileCollection stores the temporary files // generated during a build in the current directory, // and does not delete them after compilation. if (_ResourceFiles != null && _ResourceFiles.Length > 0) foreach (string _ResourceFile in _ResourceFiles) { // Set the embedded resource file of the assembly. _CompilerParameters.EmbeddedResources.Add(_ResourceFile); } try { // Invoke compilation System.CodeDom.Compiler.CompilerResults _CompilerResults = null; if (_SourceFile != null && System.IO.File.Exists(_SourceFile)) // soruce code in external file _CompilerResults = _CodeCompiler.CompileAssemblyFromFile(_CompilerParameters, _SourceFile); else // source code pass as a string _CompilerResults = _CodeCompiler.CompileAssemblyFromSource(_CompilerParameters, _SourceCode); if (_CompilerResults.Errors.Count > 0) { // Return compilation errors _Errors = ""; foreach (System.CodeDom.Compiler.CompilerError CompErr in _CompilerResults.Errors) { _Errors += "Line number " + CompErr.Line + ", Error Number: " + CompErr.ErrorNumber + ", '" + CompErr.ErrorText + ";\r\n\r\n"; } // Return the results of compilation - Failed return false; } else { // no compile errors _Errors = null; } } catch (Exception _Exception) { // Error occurred when trying to compile the code _Errors = _Exception.Message; return false; } // Return the results of compilation - Success return true; }
Código
De modo que veo que con System.CodeDom y algo mas se puede compilar el codigo que tu le introduces a modo de una variable string.
string _Errors = ""; // C# source code pass as a string string _CSharpSourceCode = @" using System; namespace test { class Program { static void Main(string[] args) { Console.WriteLine(""Press ENTER key to start ...""); Console.ReadLine(); for (int c=0; c<=100; c++) Console.WriteLine(c.ToString()); } } }"; // Compile C-Sharp code if (CompileCode(new Microsoft.CSharp.CSharpCodeProvider(), _CSharpSourceCode, null, "c:\\temp\\C-Sharp-test.exe", null, null, ref _Errors)) Console.WriteLine("Code compiled successfully"); else Console.WriteLine("Error occurred during compilation : \r\n" + _Errors);
Mi problema es que quiero compilar un proyecto entero de c# con sus librerias y referencias.. porsupuesto en una string es impensable!
Como podria hacer para generar un exe con unas opciones y caracteristicas que yo le ponga a partir de otro exe??
vamos un builder de toda la vida xd