Why cant a .net 4.0 project reference a 4.5 assemb

2019-06-15 15:52发布

问题:

I am working on a project targeting .NET 4.0 and now I need to reference a third party dll which was built targeting .NET 4.5. It seems that visual studio does not accept that? Why? VS shows me that dll name does not exist in current context.

回答1:

Because you are referencing to the higher version. Microsoft can be backcompatible, but not "future compatible". You need to convert your entire project to higher vesion, in order to be able to use that binary.

Don't know what tecnology you are talking about exactly, but if that asp.net, consider that in vNext you are able to deploy your own version of the cloud clr on the server. So you may have 2 versions of your app, one targetting 4.5, other 4.0.

Introducing vNext. Repeat, if this concerns you.



回答2:

A project targeting .NET 4.0 can run on the 4.0 runtime. A DLL targeting .NET 4.5 cannot.

If allowed; you would be able to build projects that appear to work in 4.0, and fail when they tried to load the 4.5 components.

You can reference things going the other way; as a 4.0 DLL can be loaded in the 4.5 runtime.

You will need to either request a 4.0 version of the DLL from the third party; or target .NET 4.5.



回答3:

The application domain will be running under the 4.0 runtime and so cannot reference newer dotnet version, only the reverse is true.

4.5 is a different .net runtime so it is not possible to load a 4.5 in an older version.

You must remember that the runtime will need to interpret the Intermediate Language (IL) into machine code. If the IL was created on 4.5 a 4.0 runtime would have no idea how to interpret it.



回答4:

Create a console app in 4.5 and then execute a cmd from 4.0 to get the data you needed from 4.5, might be a quick solution. Not idea but it does the job :)



标签: c# .net-4.5