Multitargeting against .NET 2.0

If you’re using the new VS 2008 multitargeting features to target .NET 2.0 you should be aware that in VS 2008 they have a limitation related to service packs.

In .NET 2.0 SP1, the CLR team has added a few types to existing .NET 2.0 assemblies. For example, DateTimeOffset has been added to mscorlib.dll.

Since VS 2008 installs .NET 2.0 SP1 (sometimes known as “redbits”), with VS 2008 you will be able to successfully build and test an app that uses DateTimeOffset, even if you explicitly tell VS 2008 that you wish to target .NET 2.0. But then if you move this app to a machine that only has the original .NET 2.0 on it, it won’t run. This is really a limitation in the Visual Studio multitargeting implementation, at least assuming that the CLR team made the right choice in adding this to an existing assembly.

This limitation may be addressed in the next version of Visual Studio. Meanwhile, if you want your app to work on machines without .NET 2.0 SP1, make sure you verify that it compiles on such a machine. You don’t need to install Visual Studio to compile it, of course — MSBuild 2.0 can do it. If your project was created by Visual Studio 2008, it will use the property $(MSBuildToolsPath) in it, which is new in MSBuild 3.5. Also, Visual Studio 2008 updated the solution version number to something MSBuild 2.0 won’t recognize (that’s another story). To work around these issues, specify the project directly, and set a property, like this

%windir%\\framework\v2.0.50727\msbuild.exe /p:msbuildtoolspath=%windir%\\framework\v2.0.50727 myProjectCreatedByVS2008.csproj

If this builds successfully without .NET 2.0 SP1 installed, you’ll know you haven’t used one of these types.

By the way, .NET 2.0 SP1 will be available as a standalone install, or it will automatically be installed when you install .NET 3.5 or Visual Studio 2008, or Vista SP1, or “Longhorn” server.

[Author: Dan]

Update: Scott has generated a libcheck diff showing all the new types in SP1. Take a look here: 

Also, Krzysztof has created an FXCop rule to check for these changes: