Home > Back-end >  How to build Visual Studio .csproj as Mono?
How to build Visual Studio .csproj as Mono?

Time:12-01

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 Files\Mono>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.

CodePudding user response:

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).

Create a pure .NET Core console app (with dotnet new console) please, as that's recommended for cross platform scenarios.

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 use mono executable_name.exe to run it on Linux. But note that if your console application uses any Windows only API/dependencies.

MonoDevelop is totally irrelevant here.

Both Mono and MonoDevelop are going away. .NET Core is the future.

CodePudding user response:

  1. Install Mono (select there your OS)

Build your application via Visual Studio as standard build then open cmd and cd into path where is Mono located, in my case this is (Windows OS) after that - run this builded app.

C:\Program Files\Mono> as x64;
for x86 Mono located here C:\Program Files (x86)\Mono> (path where is mono located)

  1. Simply write cd C:\Program Files\Mono>
  2. mono "path_to_the_compiled_executable_in_VS.exe" (executable should have entry point, console app will be great in such case)
  • Related