views:

108

answers:

2

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.

A: 

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.

David
We wish to make sure a developer doesn't accidently/unknowingly add use any of the new features.
Alex
I would expect the compiler to catch these problems for you. If not the compiler, then any unit tests would catch it for sure.
David
+1  A: 

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.

Brett Veenstra
And how would you detect these? I have looked at the manifests, the proj files, and alike. And, without a global list of each dll and which version it is in I cannot figure out how to best determine. Good news is I have tested VS 2010 and it does enforce this, so the best option just may be to wait for 2010 and upgrade immediately.
Alex