Skip to content

Introduce a Standard $(TargetFrameworkVersionMajor) MSBuild Property #12632

@ActivistInvestor

Description

@ActivistInvestor

Summary

I propose adding a new, standard MSBuild property, $(TargetFrameworkVersionMajor), that returns only the major version number of the currently targeted framework. This would greatly simplify conditional logic within .props and .targets files in multi-targeted projects, particularly for users moving to the new SDK-style project format.

Background and Motivation

In modern .NET multi-targeting projects, the primary property most-commonly used in conditional build logic is $(TargetFramework) (e.g., net48, net6.0, net8.0, net9.0-windows, etc.). When writing conditional logic, users often need to distinguish between major framework generations only (e.g., to load different reference assemblies, set specific compiler flags, or define environment variables).

Currently, this requires using verbose, repetitive MSBuild string functions in many locations:

<PropertyGroup Condition="$([System.String]::new('$(TargetFramework)').StartsWith('net4'))">
    <FrameworkMajorVer>4</FrameworkMajorVer>
</PropertyGroup>
<PropertyGroup Condition="$([System.String]::new('$(TargetFramework)').StartsWith('net8'))">
    <FrameworkMajorVer>8</FrameworkMajorVer>
</PropertyGroup>

<Target Name="ValidateRefs" BeforeTargets="PrepareForBuild">
    <Error Condition="'$(FrameworkMajorVer)' == '8' And '$(REF_PATH_NET8)' == ''"
           Text="Reference path for .NET 8 is not set." />
</Target>

Proposed Feature

Introduce a single, read-only property:

$(TargetFrameworkVersionMajor)

$(TargetFramework) Value Proposed $(TargetFrameworkVersionMajor) Value
net462 4
net48 4
net6.0 6
net8.0 8
net8.0-windows 8
net9.0 9
net10.0 10

Benefits

Readability: Simplifies complex conditional logic, e.g.,

Condition="'$(TargetFrameworkVersionMajor)' == '8'"
Condition="'$(TargetFrameworkVersionMajor)' >= '6'"

Maintainability: Reduces the need for redundant string functions or custom property definitions across numerous build files.

Consistency: Simplifies conditional logic that only cares about the major framework version, allowing conditional testing to see different variants of the same major framework version (e.g., net10.0, net10.0-windows, etc.) as equivalent, thereby standardizing a common pattern currently implemented manually by developers.

This small addition would be a significant quality-of-life improvement for anyone maintaining complex multi-targeted codebases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions