public TaskResult <string> GenerateExecutable(string exeFullName, string sourceCode) { var result = new TaskResult <string>(); // 1.Create a new CSharpCodePrivoder instance var Compiler = new CSharpCodeProvider(); // 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance CompilerParameters Parameters = new CompilerParameters(); Parameters.ReferencedAssemblies.Add("System.dll"); Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); Parameters.GenerateExecutable = true; Parameters.GenerateInMemory = false; Parameters.MainClass = "DynamiclyProgram"; Parameters.IncludeDebugInformation = true; Parameters.OutputAssembly = exeFullName; // 3.CompilerResults: Complile the code snippet by calling a method from the provider CompilerResults cr = Compiler.CompileAssemblyFromSource(Parameters, sourceCode); if (cr.Errors.HasErrors) { return(result.AddErrors <CompilerErrorCollection, CompilerError>(cr.Errors, (error) => $"Compiling Error Line: { error.Line } - { error.ErrorText }")); } return(result.AddContent(cr.PathToAssembly)); }
public TaskResult <string> CreateAssembly(string AssemblyName, string Code, string Path = "DynamicAssemblys") { var result = new TaskResult <string>(); result.AddErrors(CompilerAssembly(AssemblyName, Code)); return(result.AddContent($"{Path}\\{AssemblyName}.dll")); }