C#C
C#15mo ago
revolt

Partial classes and platform specific code

Hello. Trying to create platform specific code and only use/compile the code for correct platform upon release. My current issue is that regardless of platform my linux specific code is trying to run on windows. I seen examples of it using MAUI, however i'm just trying to make a simple console app.
In my csproj:
 <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
  <DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
  <DefineConstants>LINUX</DefineConstants>
</PropertyGroup>

Base class:
public partial class Platform
{
    public partial void SetWallpaper(string path);
}

used like this:
#if WINDOWS
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;

public partial class Platform
    {
public partial void SetWallpaper(string path)
        {
    //implementation
  }
}
#endif


an identical implementation is done for linux and all code is in the same namespace
and it's built using this command:
dotnet publish wallpaper.csproj -r win-x64 -c Release
Was this page helpful?