0

I'm trying to compile this C# code:

    using System;
    
    namespace test
    {
        internal static class Program
        {
            static void Main()
            {
                Console.WriteLine("Test");
                Console.ReadKey(true);
            }
        }
    }

into a single .exe file, by using Roslyn:

                var syntaxTree = CSharpSyntaxTree.ParseText(source);

                var compilation = CSharpCompilation.Create("program.exe")
                    .WithOptions(new CSharpCompilationOptions(OutputKind.ConsoleApplication))
                    .AddReferences(Net60.References.All)
                    .AddSyntaxTrees(syntaxTree);

                EmitResult emitResult = compilation.Emit(Path.Combine(path, "program.exe"));

                if (emitResult.Success)
                {
                    MessageBox.Show("Compiled.");
                }
                else
                {
                    MessageBox.Show($"The compiler has encountered {emitResult.Diagnostics.Length} errors", "Errors while compiling");
                    foreach (var diagnostic in emitResult.Diagnostics)
                    {
                        MessageBox.Show($"{diagnostic.GetMessage()}\nLine: {diagnostic.Location.GetLineSpan().StartLinePosition.Line} - Column: {diagnostic.Location.GetLineSpan().StartLinePosition.Character}", "Error");
                    }
                }

There's no single error while compiling it. It compiles "correctly", but when i launch it this error popup in console: Unhandled exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The specified file could not be found.

While compiling via Visual Studio (to an .exe and a .dll) it works.

How can i get it working? I just need it to compile to one, single .exe file. Is this even possible?

1 Answer 1

1

Not with Roslyn; the .NET SDK has support for combining things into a single executable, but that's done as a part of publishing and isn't something Roslyn does directly. One thing to remember is that Roslyn is just one part of a much larger toolchain here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.