TaskResult CompilerAssembly(string AssemblyFullName, string SourceCode) { var result = new TaskResult(); // 1.Create a new CSharpCodePrivoder instance CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(); // 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance CompilerParameters objCompilerParameters = new CompilerParameters(); objCompilerParameters.ReferencedAssemblies.Add("System.dll"); objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); // objCompilerParameters.GenerateInMemory = true; // Load the resulting assembly into memory objCompilerParameters.GenerateInMemory = false; objCompilerParameters.OutputAssembly = $"{AssemblyFullName}.dll"; // 3.CompilerResults: Complile the code snippet by calling a method from the provider CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, SourceCode); if (cr.Errors.HasErrors) { return(result.AddErrors <CompilerErrorCollection, CompilerError>(cr.Errors, (error) => $"Compiling Error Line: { error.Line } - { error.ErrorText }")); } return(result); }
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")); }