When linking a .NET 2.0 Managed Assembly from a .NET 4.0 Application, which framework is used?
If I have a 2.0 CLR assembly (pure managed cod开发者_如何转开发e, no mixed mode issues) that I need to link to from a 4.0 CLR Application, does the 2.0 code run on the 2.0 CLR or 4.0.
Basically, is there any risk of 4.0 breaking changes affecting the 2.0 code?
The answer above is incorrect. You do get side by side with the full frameworks. A .Net 2 APPLICATION (note that means EXE, not library) will not auto promote to .Net 4.
But if a .Net 4 application loads a .Net 2 assembly it is loaded into the same runtime (otherwise how could they share information). The .Net 2 assembly is loaded into the .net 4 runtime using a compatibility mode that is supposed to minimize breakage in changes (mostly for security changes in .Net 4).
A .Net 2 assembly cannot reference a .Net 4 assembly because it would not have features.
The ONLY exception to this that I know of is if you load a .Net assembly from a C++ app. The C++ application can load and host two runtimes. It could have a .Net 2 assembly and a .Net 4 assembly loaded, but they would not be able to talk to each other directly. This is how CLR Procs work in SQL Server. You can have a .Net 2 CLR Proc and a .Net 4 CLR Proc that do not communicate, but are both loaded on the server.
There was a great article on MSDN Magazine about hosting the .Net framework recently, but I can't find it now. Maybe someone else can post the link.
So you should be able to load just about any .Net 2 assembly into a .Net 4 executable without much problem. The only problems I have seen are with security permissions.
Basically, is there any risk of 4.0 breaking changes affecting the 2.0 code?
Nope. Starting with .NET 4, you don't have to worry about compatibility issues at all! Version 4 introduced a new feature called "In-Process Side-by-Side Execution", which essentially allows you to load multiple versions of the CLR into the same process.
In other words, since your main application is running on the 4.0 runtime, you can tell it to load the 2.0 runtime when you load the 2.0 CLR assembly. The 2.0 CLR assembly will use the 2.0 runtime, while your application will continue to use the 4.0 runtime.
How do you specify this? I believe you can just add a config file for your 2.0 CLR assembly (e.g. "My.dll.config" in the same directory as "My.dll"), but I haven't tried it with DLLs myself. Nevertheless, here's what you put in your assembly's config file:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
</startup>
</configuration>
if the answer from htw did not answer it for you, or it crashes on you. Have a look at this
http://msmvps.com/blogs/rfennell/archive/2010/03/27/mixed-mode-assembly-is-built-against-version-v2-0-50727-error-using-net-4-development-web-server.aspx
精彩评论