I need to be able to target .NET Framework 4.0 using Visual Studio 2012 and verify that my code will work correctly when deployed on our 4.0 environment (Windows Server 2003).
The multi-targeting in Visual Studio 2012 seems to work properly, but only for mscorlib.dll
. When referencing any other framework DLL, for compilation you get proper errors for e.g. referencing a type that doesn't exist in 4.0, but the 4.5 version of the DLL is loaded during execution and debugging.
This makes it impossible to verify that my code will work properly in the production environment taking into account the breaking changes in the in-place upgrade that the 4.5 version of the framework did.
I made some unit tests to test the multi-targeting functionality by exercising some of the differences between 4.0 and 4.5 found on MSDN. The tests are contained in their own projects targeted at the version of the framework that they're testing. All tests should pass.
Tests against MSCORLIB
These tests pass successfully as List<string>
is located in mscorlib.dll
:
Framework 4.0: -passes-
[TestMethod]
public void List_Foreach_should_not_throw_if_list_is_modified() {
var list = new List<string> { "This", "here", "be", "a", "list", "of", "strings" };
list.ForEach((s) => {
if (s.Equals("be", StringComparison.OrdinalIgnoreCase)) {
list.Add(".");
}
});
}
Framework 4.5: -passes-
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void List_Foreach_should_throw_if_list_is_modified() {
var list = new List<string> { "This", "here", "be", "a", "list", "of", "strings" };
list.ForEach((s) => {
if (s.Equals("be", StringComparison.OrdinalIgnoreCase)) {
list.Add(".");
}
});
}
Tests against other framework DLLs
These tests however do not work correctly (the 4.5 one passes, the 4.0 one doesn't) since these types are found in System.ComponentModel.Composition.dll
and the 4.5 version is always loaded:
Framework 4.0 -fails, throws the exception expected on 4.5-
[TestMethod]
public void Should_be_able_to_create_a_serializer_for_MEF_catalogs()
{
var catalog = new AggregateCatalog();
var serializer = new XmlSerializer(typeof(AggregateCatalog));
}
Framework 4.5 -passes-
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Should_not_be_able_to_create_a_serializer_for_MEF_catalogs()
{
var catalog = new AggregateCatalog();
var serializer = new XmlSerializer(typeof(AggregateCatalog));
}
Is this as designed? It seems disjoint considering the 4.0 version of mscorlib is loaded but the 4.5 version of every other assembly.
Is there a way to get my desired functionality?
Update
Here are the solution/projects that I'm using.