-1

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.

Hertzole
  • 73
  • 8

1 Answers1

0

As Eric Lippert pointed out, I was using a newer .NET version than the compiler. So I simply changed the compiler to the new Roslyn compiler that supports C# 6, which I just what I need. So lesson learned: Always make sure you target the right framework version.

OLD ANSWER

Welp, I'm going to declare myself idiot once again. Better to do it myself before anyone else does.

I got it working by changing the Program.cs file.

Instead of

static void Main(string[] args) => new MyBot().RunAsync().GetAwaiter().GetResult();

I did

static void Main(string[] args)
{
    new MyBot().RunAsync().GetAwaiter().GetResult();
}

But I still have no idea why it didn't work in the first place since it worked in Visual Studio.

Hertzole
  • 73
  • 8
  • You are using C# 7 in Visual Studio and some earlier version of C# in whatever other environment you are compiling in. – Eric Lippert May 12 '17 at 17:12
  • @EricLippert Yeah, I kinda discovered that now when it became a little bit clearer. I need to run .NET 4.6+ code but the compiler doesn't seem like it can target that. Further research is needed on my part. – Hertzole May 12 '17 at 17:20