springy76
springy76

Reputation: 3786

How can the very same assembly behave differently when used by an 4.5 or 4.6 app?

A quote out of What's New in 4.6

For apps that target the .NET Framework 4.6 RC, Task and Task<TResult> objects inherit the culture and UI culture of the calling thread. The behavior of apps that target previous versions of the .NET Framework, or that do not target a specific version of the .NET Framework, is unaffected.

I always thought that there only exists v4.0.30319 at the assembly level for 4.0, 4.0.1, 4.0.2, 4.0.3, 4.5, 4.5.1, 4.5.2 and 4.6 -- and that 4.6 is only an inplace update for 4.0-4.5.2?

To my current knowledge the specified version in the app.config file only triggers a message box if the SKU is not installed at all -- how can this affect how assemblies behave?

Upvotes: 2

Views: 420

Answers (1)

Lex Li
Lex Li

Reputation: 63203

This page says it all,

https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx

Culture and task-based asynchronous operations

The task-based asynchronous programming pattern uses Task and Task objects to asynchronously execute delegates on thread pool threads. The specific thread on which a particular task runs is not known in advance, but is determined only at runtime.

For apps the target the .NET Framework 4.6 RC or later versions, culture is part of an asynchronous operation's context. In other words, starting with apps the target the .NET Framework 4.6 RC, asynchronous operations by default inherit the values of the CurrentCulture and CurrentUICulture properties of the thread from which they are launched. If the current culture or current UI culture differs from the system culture, the current culture crosses thread boundaries and becomes the current culture of the thread pool thread that is executing an asynchronous operation.

The following example provides a simple illustration. It uses the TargetFrameworkAttribute attribute to target the .NET Framework 4.6 RC. The example defines a Func delegate, formatDelegate, that returns some numbers formatted as currency values. The example changes the current system culture to either French (France) or, if French (France) is already the current culture, English (United States). It then:

  • Invokes the delegate directly so that it runs synchronously on the main app thread.
  • Creates a task that executes the delegate asynchronously on a thread pool thread.
  • Creates a task that executes the delegate synchronously on the main app thread by calling the Task.RunSynchronously method.

You can see from the code sample to see how to explicitly mark your program as .NET 4.6 targeted. Then CLR uses the new behavior. For all assemblies without such an attribute, or the value is not 4.6, the old behavior is kept.

Upvotes: 1

Related Questions