You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
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
@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.
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.
And code of my compiler.
After generation of exe file in the file system (for experiment) I placed that exe to
Microsoft.NETCore.App
folder with alldlls
, to confirm that it works.Running generated exe file:
Expected Behavior:
Actual Behavior:
For:
3.0.100-preview6-012264
I really need help with it.
Thanks a lot!
The text was updated successfully, but these errors were encountered: