I'm trying to compile multiple C# files into one executable at runtime and I keep getting compiler errors that aren't present if I run this code from Visual Studio.
The specific errors I'm getting from my runtime compiler is "; expected" and "Method must have a return type" at line 7 in 'Program.cs'. Obviously, I would fix these if there was something to fix.
So the code. Here's the actual compiler code: (with some parts removed, like the actual error reporting)
private CSharpCodeProvider provider = new CSharpCodeProvider();
private CompilerParameters parameters = new CompilerParameters();
private void RunOption_Click(object sender, RoutedEventArgs e)
{
parameters.GenerateInMemory = false;
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Path + "/builds/debug/bot.exe";
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Threading.Tasks.dll");
parameters.CompilerOptions = "/optimize";
parameters.WarningLevel = 3;
parameters.TreatWarningsAsErrors = false;
string[] filesToCompile = Directory.GetFiles(Path + "\\src\\", "*.cs");
CompilerResults results = provider.CompileAssemblyFromFile(parameters, filesToCompile);
}
And here are the two files it's trying to compile.
Program.cs
using System.Threading.Tasks;
namespace MyBot
{
class Program
{
static void Main(string[] args) => new MyBot().RunAsync().GetAwaiter().GetResult();
}
}
MyBot.cs
using System;
using System.Threading.Tasks;
namespace MyBot
{
public class MyBot
{
public async Task RunAsync()
{
Console.Title = "MyBot";
Console.Read();
}
}
}
What am I missing here? As mentioned, if I run the code from 'Program.cs' and 'MyBot.cs' in Visual Studio, everything works fine, so there are no errors in the actual code.