the_drow
the_drow

Reputation: 19171

How to detect on compile time if the target .NET runtime is 4.0 or above?

I have read this question but now that we also have 4.5 the solution won't work.
How can I check if the runtime is above 4.0?
Since it's a string I am guessing I cannot do:

<DefineConstants Condition=" '$(TargetFrameworkVersion)' >= 'v4.0' ">NET_4_0</DefineConstants>

So should I define both:

<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.0' ">NET_4_0</DefineConstants>
<DefineConstants Condition=" '$(TargetFrameworkVersion)' == 'v4.5' ">NET_4_5</DefineConstants>

Is there a more generic solution?

Upvotes: 1

Views: 189

Answers (2)

CAD bloke
CAD bloke

Reputation: 8798

Carrying on from @MichaelPrice's answer & @HansPassant's comment...

<Choose>
  <When Condition=" '$(TargetFrameworkVersion.Substring(1,3))'&gt;'3.5' ">
    <ItemGroup>
      <DefineConstants>$(DefineConstants);NEWERTHANNET35</DefineConstants>
    </ItemGroup>
  </When>
</Choose>

Upvotes: 0

Michael Price
Michael Price

Reputation: 8968

You can utilize any methods from the System.String class in the .NET Library, including comparisons.

See http://msdn.microsoft.com/en-us/library/dd633440.aspx

Edit

And if you can't get property functions to work well enough for you, you could try the MSBuild Extension Pack. http://www.msbuildextensionpack.com/help/3.5.3.0/html/9c5401ed-6f55-089e-3918-2476c186ca66.htm

Upvotes: 2

Related Questions