Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:28:24+00:00 2026-06-11T22:28:24+00:00

I need to be able to target .NET Framework 4.0 using Visual Studio 2012

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:28:25+00:00Added an answer on June 11, 2026 at 10:28 pm

    Thanks for the great repro!

    When looking at your project there were two things that are affecting your results leading to the behavior you are seeing:

    1) When we detect a 4.0 application running under 4.5, we “shim” changed APIs to return the old 4.0 behavior when we consider it observable from reasonable (ie not contrived) application usage. For example, a 4.0 application that relied on the ability to modify the list in List.ForEach will continue to see the 4.0 behavior, regardless of whether running under 4.0 or 4.5. 4.5 applications will see the new behavior.

    To determine what we consider reasonable usage, and to guide the APIs we shim, we have a compatibility team that is responsible for looking over every single “breaking” change across the entire framework, and comparing it against set of rules and guidelines that we’ve built up over the past 10 years.

    In the project you attached, there are 5 things you are testing:

    • List.ForEach under 4.5 throws InvalidOperationException when the list is modified
    • Uri under 4.5 now preserves tailing dots in path segmants
    • Uri under 4.5 no longer escapes ? in file://-based URIs
    • Enumerable.Empty under 4.5 now guarantees that it returns the same instance (this one is a little misleading in the docs [I’ve filed a bug], I believe the behavior was on 4.0 that it could return two different instances the first time it was accessed by two different threads on a multi-proc’d machine at the same time)
    • MEF’s catalogs are no longer XML serializable

    Of these behaviors, the first three are shimmed to return the previous behavior on 4.5 when a 4.0 application is running. Whereas, the last two are not shimmed. This is because it would take very contrived application usages to be broken by the last two changes, in which case, we simply document them on that above as an FYI. For example, take the MEF catalog change, while you theoretically you could serialize these types using the XML serializer (which was unintentional, and IMHO a terrible behavior of the XML serializer) you could not do anything with the result, as it didn’t deseralize to anything useful.

    I’m chasing up if we can modify that page to include the list of breaking changes that are shimmed for a 4.0 application.

    2) The second issue that I ran into was that 4.0 test project was incorrectly being detected as 4.5 when they were in the same run as another 4.5-based test project. You can observe this by running the 4.0 tests by themselves and the three shimed ones will pass. Run them alongside the 4.5 tests and they fail. I’m not sure where this issue is coming into play, but I’ve filed a bug internally and started up a thread with the responsible team to get to the bottom of it. If you would like to keep track of the issue externally, feel free to file a bug over on http://connect.microsoft.com/VisualStudio and it we’ll route to the correct owners.

    David Kean

    CLR Team

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to copy a file that exists on the target
I need to change the .NET Framework Version of my Sharepoint Project from 3.5
Can programs build in VS2012 with platform toolset Visual Studio 2012 (v110) run on
I need to be able to read the target of a shortcut (a .lnk
Environment: C# projects, Visual studio 2008, C#, .Net 3.5, MSBuild Objective: Run my own
I'm looking for a library that can be used in native .NET code, just
I need to be able to get the href (or somehow get the target
I need to be able to post with targets set (country, language, city and
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.