Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build runnable .NET Core Console App using Roslyn #37206

Closed
DanTsk opened this issue Jul 13, 2019 · 4 comments
Closed

Build runnable .NET Core Console App using Roslyn #37206

DanTsk opened this issue Jul 13, 2019 · 4 comments

Comments

@DanTsk
Copy link

DanTsk commented Jul 13, 2019

Version Used:
3.0.100-preview6-012264

I am trying to perform compilation to an exe file using Roslyn.
I have used this code to a source for compilation.

namespace Compiler
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
        }
    }
}

And code of my compiler.

namespace Compiler
{
    public class Compiler
    {
        private CSharpCompilation _compilation;

        public Compiler(string assamblyName, string[] sources) {
            var syntaxTrees = ParseSyntaxTrees(sources);

            _compilation = CSharpCompilation.Create(assamblyName, syntaxTrees, GetMetadataReference(), GetCompilationOptions());
        }

        public EmitResult Emit(string filePath)
        {
            return _compilation.Emit(filePath);
        }

        private IEnumerable<SyntaxTree> ParseSyntaxTrees(string[] sources)
        {
            return sources.Select(source => CSharpSyntaxTree.ParseText(source));
        }

        public IEnumerable<MetadataReference> GetMetadataReference()
        {
            var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);

            Console.WriteLine(assemblyPath);

            return new[] {
                MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Private.CoreLib.dll")),
                MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Console.dll")),
                MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")),
            };
        }

        private CSharpCompilationOptions GetCompilationOptions()
        {
            return new CSharpCompilationOptions(OutputKind.ConsoleApplication)
                    .WithOverflowChecks(true)
                    .WithOptimizationLevel(OptimizationLevel.Debug);
        }
    }
}

After generation of exe file in the file system (for experiment) I placed that exe to Microsoft.NETCore.App folder with all dlls, to confirm that it works.

Running generated exe file:
Expected Behavior:

Hello

Actual Behavior:
For: 3.0.100-preview6-012264

Unhandled Exception: System.TypeLoadException: Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does not exist.

I really need help with it.
Thanks a lot!

@DanTsk
Copy link
Author

DanTsk commented Jul 13, 2019

And one more small question is there ability to build single executable file without referencing dlls? Like in .NET Core 3.0 CLI PublishSingleFile=true

@agocke
Copy link
Member

agocke commented Jul 14, 2019

@DanTsk I believe it's considered unsupported by .NET Core to compile directly against System.Private.CoreLib.dll. In general, it will probably be quite difficult to compile an application without using the dotnet tool and MSBuild, as you will have to effectively replicate all the logic that goes into picking the appropriate reference assemblies, then generating the right executing environment for the .NET Core shared loader.

@DanTsk
Copy link
Author

DanTsk commented Jul 17, 2019

@agocke Thanks for pointing, looks like dotnet tool perfectly solves my needs.

@DanTsk DanTsk closed this as completed Jul 17, 2019
@agyen
Copy link

agyen commented Sep 8, 2020

@DanTsk please how did the dotnet tool helped you I'm having the same problem. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants