Search code examples
c#.netmultitargeting

Adding a reference for specific platform on multi-platform project


Trying to figure out to use System.Diagnostics.EventLog in a multi-target project. The targets are:

  • .NET 5
  • .NET Framework 4.8

I have 3 projects in my solution and everything works fine on the 2 others. The one raising up problems is referencing System.Diagnostics.EventLog for .NET 5 since it's required to use EventLog. On the .NET framework 4.8, System.Diagnostics.EventLog namespace uses System.dll.

I'm looking for a way to include a reference to this library only in .NET 5 project, and not in the 4.8 one.

When I ad the reference to the project, it seems like it adds it to both targets. Is there a way to add it to the .NET 5 and not to the .NET framework 4.8?


Solution

  • Aftfer many tries, seek and (almost) destroy, I finally realized it was really simple:

    Editing the project file and giving a conditional compilation argument on the reference which was required only for the .NET 5 target:

    <ItemGroup Condition=" '$(TargetFramework)' == 'net5.0-windows' ">
      <Reference Include="System.Diagnostics.EventLog">
        <HintPath>C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\5.0.0\ref\net5.0\System.Diagnostics.EventLog.dll</HintPath>
      </Reference>
    </ItemGroup>
    

    That's it!