本文介绍了如何在VS2008中的项目目标3.5中引用框架4.0 dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS2010中使用4.0框架创建了一个dll(是的,完整的4.0,而不是客户端配置文件)。我想在VS2008项目中引用它(意味着我无法将引用项目重定向到4.0,即使我想),并且将该项目升级到VS2010是不是一个选项。



我明白为什么在3.5项目中引用4.0项目有问题,但是我不明白为什么在3.5项目中引用4.0 dll的问题。所有我知道这可能不是问题,但我似乎无法想到还有什么可能。



我正在尝试使用Fasterflect(http ://fasterflect.codeplex.com/),它依赖于3.5项目中的4.0功能(再次,我的公司还没有准备好为VS2010提供资金,所以我不能改变)。一切似乎都开始好起来,直到我尝试编译然后错误的Fasterflect组件没有被签名弹出。所以我下载了实际的源代码,签名,重新编译,并引用了我新签署的程序集。问题是现在它显示警告符号,并说:

尝试按照这里建议的答案:。但是,当我这样做时,我收到另一个错误消息:

我似乎也不会想出错误。



请确保在VS2008中使用这个dll吗?

解决方案

当您创建一个针对特定版本的框架的项目时,您会告诉编译器将运行该项目的计算机的哪个版本的框架库和运行时版本。例如,3.5中的System.dll和4.0中的System.dll不一样,很像为2.0和3.5之间的System.dll不一样。



理论上,您不能保证转发兼容性(3.5引用4.0),因为4.0程序集可能使用3.5中不存在的API。同样,您不能保证4.0中的向后兼容性,因为某些API可能已被弃用和删除。



在实践中,4.0具有足够的向后兼容性3.5,它们允许您在4.0应用程序中引用3.5组件,并附带一些注意事项(您有时需要在您的配置文件中,因为4.0中的装配加载从3.5更改)。不幸的是,由于4.0引入的大量新API,3.5与4.0没有很大的向前兼容性,所以我不认为你可以从3.5引用4.0程序集。



您可能会看到该模式信息异常。 supportedRuntime是由.NET 4.0引入的配置元素。但是,由于您在3.5中引用程序集,所以解析配置文件的.NET 3.5版本的System.Configuration不能识别该元素,从而引发异常。



唯一的方法可能是(1)更改源并重新编译,将所有的API调用修复到4.0中不工作的4.0库,或者(2)做同样的事情,但是在IL级别,生成一个新程序集(可以使用al.exe执行此操作)。


I have a dll created in VS2010 using the 4.0 framework (yes it's the full 4.0, NOT the client profile). I would like to reference it in a VS2008 project (meaning I can't re-target the referencing project to 4.0 even if I wanted to) and upgrading that project to VS2010 is not an option.

I understand why there are issues with referencing 4.0 'projects' in 3.5 projects, but I don't understand why I should have a problem referencing a 4.0 dll in a 3.5 project. And for all I know this may not be the issue, but I can't seem to think of what else it could be.

I am trying to use Fasterflect (http://fasterflect.codeplex.com/) which relies heavily on 4.0 features in my 3.5 project (and again, my company is not ready to shell out the money for VS2010 so I can't change that). Everything seemed to be going okay at first until I tried to compile then errors about the Fasterflect assembly not being signed popped up. So I downloaded the actual source code, signed it, recompiled it, and referenced my newly signed assembly. The problem is that now it displays the caution symbol and says:

I tried following the answer suggested here: http://social.msdn.microsoft.com/Forums/en/clr/thread/36b1a209-55d5-4323-91dc-0919ba2e1d03. However, I get another error message when I do that:

And I can't seem to figure that error out either.

Surely there has to be SOME way for me to use this dll in VS2008??

解决方案

When you create a project that targets a specific version of the framework, you're telling the compiler which version of the framework libraries and runtime that the computer on which the project will run has installed. For example, System.dll in 3.5 and System.dll in 4.0 aren't the same, much like why System.dll in 2.0 and 3.5 aren't the same.

In theory, you can't guarantee forwards compatibility (3.5 referencing a 4.0) because the 4.0 assembly may use APIs that aren't present in 3.5. Likewise, you can't guarantee 2.0 backwards compatibility in 4.0 because some of the APIs may have been deprecated and removed.

In practice, 4.0 has enough backwards compatibility with 3.5 that they allow you to reference 3.5 assembles in 4.0 applications, with some caveats (you sometimes need to add a directive in your configuration file, since assembly loading in 4.0 changed from 3.5). Unfortunately, 3.5 doesn't have much forwards compatibility with 4.0 due to the massive number of new APIs that 4.0 introduced, so I don't think that you can reference 4.0 assemblies from 3.5.

You're seeing that schema information exception for probably this reason. the "supportedRuntime" is a configuration element that was introduced with .NET 4.0. But since you're referencing the assembly in 3.5, the .NET 3.5 version of System.Configuration, which parses the configuration file, doesn't recognize that element and thus throws an exception.

The only way is probably to (1) change the source and re-compile, fixing all the API calls to 4.0 libraries that don't work in 3.5, or (2) do the same thing, but at the IL level, generating a new assembly (you can do this with al.exe).

这篇关于如何在VS2008中的项目目标3.5中引用框架4.0 dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 07:18