Search code examples
msbuildmono.net-6.0.net-7.0

How To Set An MSBuild Condition on the MSBuild Version


Is there a Condition I can set to change the TargetFrameworks on a given project depending on the version on MSBuild itself?

I have a solution which is a mixture of .NetFramework 4.8, NetStandard2 and .net 6 libraries.

I'm trying to get the solution building under Mono on a Linux host but I have to select a specific MSBuild to use in Rider - I can choose 17 from net 7 which will build the net core stack or 15 from Mono which builder the net48 libraries - Neither will do both.

To work around this, I'm trying to adapt the project files to adjust the TargetFrameworks property appropriately. A bit like this...

    <TargetFrameworks Condition="'$(MSBuildVersion)' == '15'">netstandard2.0;net48</TargetFrameworks>
    <TargetFrameworks Condition="'$(MSBuildVersion)' != '15'">netstandard2.0;net48;net6</TargetFrameworks>

From a pseudo-logic perspective, this would let the code build correctly and just ignore any code building for the net6 builds.

For now I'm doing this...


    <TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard2.0;net48</TargetFrameworks>
    <TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">netstandard2.0;net48;net6</TargetFrameworks>
    ```

Solution

  • The actual condition check should be against MSBuildAssemblyVersion.

        <TargetFrameworks Condition="'$(MSBuildAssemblyVersion)' == '17.0' ">net6.0;netstandard2.0;net471</TargetFrameworks>
        <TargetFrameworks Condition="'$(MSBuildAssemblyVersion)' == '16.0' ">netstandard2.0;net471</TargetFrameworks>
    

    Copied from

    https://github.com/lextudio/sharpsnmplib/blob/12.5.1/SharpSnmpLib/SharpSnmpLib.csproj

    But seriously, migrate away from Mono when you can.