Can I generate .NET Core assembly with a Roslyn running under .NET Framework #48273
-
I would like to compile a .NET Core executable by invoking Roslyn APIs. My "compiler" application is built for .NET Framework, and references Roslyn NuGet packages. So far the assemblies generated by Roslyn as a result were targeted for .NET Framework. And I didn't find a switch to change that. Is that possible, or do I need to use .NET Core version of Roslyn to generate .NET Core executables? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
See #49030 |
Beta Was this translation helpful? Give feedback.
-
Not sure if this problem is still relevant to you, but I randomly remembered seeing this question and forgetting to answer it. I believe the only meaningful* difference between a .NET Core assembly and a .NET Framework assembly is which BCL assemblies they reference. As such there's nothing preventing Roslyn running under .NET Framework from generating .NET Core assemblies. (Visual Studio runs Roslyn under .NET Framework, at least in some scenarios.) If you just want tools like ILSpy to identify your assembly as .NET Core, you need to add In the case of executables .NET Framework is somewhat different in that the assembly is generated as an .exe directly, but in .NET Core the assembly is still emitted as a .dll and the .exe is actually an apphost that launches your assembly. So if you want to generate .NET Core executables, you need to generate that apphost. When you build with the SDK, this is normally handled by the As far as locating the template for the apphost, that I'm not 100% sure about. You can see the logic of how the SDK does it in the previously linked targets file if you want to try and adapt that to your program. You could also build it from source and carry it with your application. If you just want to hard code a path for testing, on Windows the .NET 5 RC2 apphost is located at (*Obviously newer features might be used in .NET Core assemblies too. Roslyn usually (always?) determines the ability to use these features via |
Beta Was this translation helpful? Give feedback.
Not sure if this problem is still relevant to you, but I randomly remembered seeing this question and forgetting to answer it.
I believe the only meaningful* difference between a .NET Core assembly and a .NET Framework assembly is which BCL assemblies they reference. As such there's nothing preventing Roslyn running under .NET Framework from generating .NET Core assemblies. (Visual Studio runs Roslyn under .NET Framework, at least in some scenarios.)
If you just want tools like ILSpy to identify your assembly as .NET Core, you need to add
TargetFrameworkAttribute
to your code. (This is normally done automatically by MSBuild.)In the case of executables .NET Framework is somewhat different…