1

My goal is to create a console application using Roslyn programmatically:

string codeToCompile = @"
using System;
public class Program
{
    public static void Main()
    {
        Console.WriteLine(""Hello World!"");
        Console.ReadLine();
    }
}";

// Parsing the code into the SyntaxTree
var syntaxTree = CSharpSyntaxTree.ParseText(codeToCompile);
var dotNetCoreDir = Path.GetDirectoryName(typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.Location);

var refPaths = new[] {
    typeof(Object).GetTypeInfo().Assembly.Location,
    typeof(Console).GetTypeInfo().Assembly.Location,
    Path.Combine(dotNetCoreDir, "System.Runtime.dll")
};

var references = refPaths.Select(r => MetadataReference.CreateFromFile(r)).ToArray();

// Compiling
var compilation = CSharpCompilation.Create(Path.GetRandomFileName())
                                   .WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
                                   .AddReferences(references)
                                   .AddSyntaxTrees(syntaxTree);

compilation.Emit("output.exe", "output.pdb");

   

However the generated output.exe doesn't work as Windows executable:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.

But it does seem to work using the Dotnet tool:

dotnet output.exe // Hello World!

My understanding: .NET Core assemblies cannot be invoked directly, which is why I wasn't able to execute the assembly in the first case. Instead, I must use a CoreCLR host.

My question: would it be possible to modify my approach to create an executable that can be run directly?

Rafi Henig
  • 5,950
  • 2
  • 16
  • 36
  • 1
    It's basically producing a dll file. So in simple case you can just make an exe file, which starts `dotnet` with argument being your emitted dll file. – WhiteBlackGoose Sep 14 '22 at 06:41
  • @WhiteBlackGoose I would like my app to be self-contained, the user might not have the dotnet installed – Rafi Henig Sep 14 '22 at 06:44
  • Rather than re-create everything, why not use the tools as they were designed to be used. Write all the files a project requires into a folder & publish it. – Jeremy Lakeman Sep 29 '22 at 05:08

0 Answers0