Search code examples
c#compilation

Compiling exe or dll by webserver


I'd like to compile my source (or a part of it) by one of my webservers (like some websites offer nightly builds of their program). As I want my program to be customized by a third party and they have their own standalone application with say their logo and some custom strings in it. My preferable solution would be a dll file which would be loaded into my application, so I can still update the main application while retaining the customization by the third party.

So, the third party goes to my website, enters some fields and a dll file is generated (or do you have any other better way to do this?) and the dll file will be included by the application, which will grab the logo resource and some strings from it, to show in the application.

How can this be done? I'd rather to use Linux to build it, but if Windows is easier then that's not a problem either.


Solution

  • You can use the CSharpCodeProvider API for that, here is an example:

    var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
    var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
    parameters.GenerateExecutable = true;
    CompilerResults results = csc.CompileAssemblyFromSource(parameters,
    @"using System.Linq;
        class Program {
          public static void Main(string[] args) {
            var q = from i in Enumerable.Rnge(1,100)
                      where i % 2 == 0
                      select i;
          }
        }");
    
    results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
    

    If you want to use linux take a look at Mono MCS C# Compiler