Alex
Alex

Reputation: 13229

How can I enforce a specific version of the .net framework

We were under the impression that setting the target framework on the properties of a solution would limit that app to using only that framework or below's functionality. We just found out though, that someone can add a reference and start using code from a higher versioned framework and the compiler won't complain one bit. Since we would like to prevent this in the future does anyone have any ideas on how I could detect something referencing a higher version or not? I need to fail the build if someone adds code above our target.

Upvotes: 3

Views: 314

Answers (2)

Brett Veenstra
Brett Veenstra

Reputation: 48474

Assuming you're targeting .NET 2.0 and above... your build could fail if you detect references to System.Core or other 3.x assemblies (e.g. WPF) in References of your projects.

UPDATE

You could start with checking inside each .PROJ file for:

<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>

Then, inside the <ItemGroup> tag:

<Reference Include="System.Core">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
  <RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>

This could be a custom NAnt task or write your own parser to look for these nodes and fail the build.

Upvotes: 1

David
David

Reputation: 34563

Why do you need to prevent this if it works? As long as you don't use any of the new features, I believe it is possible to have "forward" compatible DLLs.

Upvotes: 0

Related Questions