I`m using Visual Studio 2022.
The goal
Run Console application (.NET Framework) – on Mono (without Unity or other tools).
Console Application code:
internal class Program
{
private static Task Main(string[] args)
{
if (Type.GetType("Mono.Runtime") != null)
{
Console.WriteLine("Mono!"); // Should be outputted 'Mono!' in console
}
else
{
Console.WriteLine("Something other!");
}
return Task.CompletedTask;
}
}
Console Application .csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>none</DebugType>
<DefineConstants>$(DefineConstants)TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DefineConstants>$(DefineConstants)TRACE</DefineConstants>
</PropertyGroup>
</Project>
My attempts:
MonoHelper extension which is available only on super old versions of Visual Studio and probably deprecated because there`s no support.
Mono msbuild
Getting error after executing this line in cmd
:
C:Program FilesMono>msbuild "path_to_project_here.csproj" -p:Configuration=Release
"path_to_project_here.csproj" (default target) (1) ->
path_to_project_here.csproj : error MSB4237: The SDK resolver type "Dot
NetMSBuildSdkResolver" failed to load. The type initializer for 'Microsoft.DotNet.DotNetSdkResolver.VSSettings' threw a
n exception.
xbuild which is deprecated also, instead used Mono msbuild
Probably I wont be use MonoDevelop
for such things, there`s should be easy way.
2
Answers
OS
)Build your application via Visual Studio as standard build then open
cmd
and cd into path where isMono
located, in my case this is (Windows
OS
) after that - run this builded app.C:Program FilesMono>
as x64;for x86
Mono
located hereC:Program Files (x86)Mono>
(path where is mono located)cd C:Program FilesMono>
It gives you not much benefit to convert .NET Framework only projects to the SDK style, as that adds up build time dependency on .NET Core SDK (you didn’t install).
Please install .NET Core SDK (.NET 6 or 7) and then use
dotnet build
to compile such projects. Then .NET CLI uses .NET Framework reference assemblies to build the executable and you can usemono executable_name.exe
to run it on Linux. But note that if your console application uses any Windows only API/dependencies.