简体   繁体   English

如何将.Net3.5 dll添加到.Net2.0项目中?

[英]How to add .Net3.5 dll into .Net2.0 project?

I have a dll which is based on .net 3.5 -- it uses internally for example Linq, but the exposed API is straightforward, no fancy stuff. 我有一个基于.net 3.5的dll - 它在内部使用例如Linq,但是暴露的API很简单,没有花哨的东西。 Since C# generics are resolved at compile time I assume that for calling party all it counts is API (all public parts). 由于C#泛型在编译时被解析,我认为对于调用方而言,所有重要的是API(所有公共部分)。

However when I try to use this dll from net2.0 project I get info, that the dll cannot be referenced because the dll or one of its dependencies requires a later version of .net framework. 但是,当我尝试使用net2.0项目中的这个dll时,我得到的信息是dll无法引用,因为dll或其中一个依赖项需要更高版本的.net框架。

I can install any .net version I want on target computer (when entire app is installed), but I cannot change .net version for the project itself. 我可以在目标计算机上安装我想要的任何.net版本(当安装整个应用程序时),但我不能为项目本身更改.net版本。

So: how to solve this? 那么: 如何解决这个问题? When adding a C dll to this project I had no such problems, so are C# dlls self-contained or not? 在这个项目中添加C dll时,我没有遇到过这样的问题,C#dll是否自包含?

C# dlls need to have the .Net runtime to run as they are not compiled down to machine code. C#dll需要运行.Net运行时才能运行,因为它们不会编译为机器代码。 In this case the dll says it requires Net 3.5 so all your project will have to use 3.5 or higher. 在这种情况下,dll表示它需要Net 3.5,因此您的所有项目都必须使用3.5或更高版本。

To keep your project as Net 2.0 you would need to build another executable to contain the 3.5 DLL and communicate across separate processes. 要将项目保持为Net 2.0,您需要构建另一个可执行文件以包含3.5 DLL并在不同的进程之间进行通信。

The C DLL worked as it is compiled down to native code and does not require the .Net framework. C DLL在编译为本机代码时工作,不需要.Net框架。 (or at least not version higher than 2.0) (或至少不高于2.0的版本)

I've been using System.Core and the new System.Web.Extensions (for example) from 3.5 in an ASP.NET 2.0 app (using VS2005) for a while now with no problems. 我已经在ASP.NET 2.0应用程序(使用VS2005)中使用System.Core和新的System.Web.Extensions (例如)一段时间而没有任何问题。 Similar to what Scott Hanselman blogged about here . 类似于Scott Hanselman在这里发表的博客。 So yes, it's possible. 所以是的,这是可能的。

.NET 3.5 still runs on the same CLR as .NET 2.0. .NET 3.5仍然在与.NET 2.0相同的CLR上运行。 So at runtime it's all the same. 所以在运行时它们都是一样的。 (Assuming you've tracked down any dependencies and copied those 3.5 DLLs to your bin folder as well.) (假设您已经跟踪了所有依赖项并将这些3.5 DLL复制到您的bin文件夹中。)

The only real limitation is what C# language features you can use at development time. 唯一真正的限制是您可以在开发时使用的C#语言功能。 Such as 'var', extension methods, or LINQ query syntax. 例如'var',扩展方法或LINQ查询语法。

If you are using linq to objects, then you can use Linq Bridge: http://www.albahari.com/nutshell/linqbridge.aspx 如果你使用linq对象,那么你可以使用Linq Bridge: http//www.albahari.com/nutshell/linqbridge.aspx

this is a Linq to objects implementation for .net 2.0. 这是针对.net 2.0的Linq to objects实现。

You will still have to compile using vs2008 but you can compile with .net 2.0 as a target platform in that case. 您仍然需要使用vs2008进行编译,但在这种情况下您可以使用.net 2.0作为目标平台进行编译。 (This is because the C# 3 compiler understands linq clauses even if you target .net 2.0, it will simply resolve the calls to linqbridge instead of the .NET 3.5 libraries in this case) (这是因为C#3编译器理解linq子句,即使你的目标是.net 2.0,在这种情况下它只会解析对linqbridge而不是.NET 3.5库的调用)

If you're using .NET 3.5 libraries then your application's requirements should be such that any consumer of it's API's should also be using .NET 3.5. 如果您使用的是.NET 3.5库,那么您的应用程序的要求应该是这样的,它的API的任何消费者也应该使用.NET 3.5。

The only way you can bypass this is if you package all the dependencies of your application along with it. 绕过这一点的唯一方法是将应用程序的所有依赖项与它一起打包。 This means libraries your application uses which depend on the .NET 3.0 and 3.5 frameworks. 这意味着您的应用程序使用的库依赖于.NET 3.0 3.5框架。

However, I'm not sure of the legality of ripping out chunks of the .NET frameworks and packaging them with an app. 但是,我不确定破解.NET框架块并用应用程序打包它们的合法性。 I'd read the EULA before doing anything like this. 在做这样的事之前,我已经阅读了EULA。 IMO, it's not worth the hassle; 国际海事组织,这不值得麻烦; just install 3.5, ask your users to install 3.5 and be done with it or use only 2.0 features and libraries. 只需安装3.5,请您的用户安装3.5并完成它或仅使用2.0功能和库。 At the very least, hacking around like this will only cause you more pain with deployment if there are framework updates in the future. 至少,如果将来有框架更新,那么这样的黑客行为只会让您在部署时更加痛苦。

In either case, your app will work on .NET 2.0 as 3.0 and 3.5 are just extra libraries on top of the 2.0 runtime and libraries (as Craig mentioned) as long as all your dependencies are there. 在任何一种情况下,你的应用程序都可以在.NET 2.0上运行,3.0和3.5只是2.0运行时和库之上的额外库(如Craig所提到的),只要你的所有依赖项都在那里。

C# DLLs are not self-contained. C#DLL不是自包含的。 If your 3.5 DLL needs LINQ, it depends on system assemblies from the 3.5 (3.0 to be exact) framework, therefore the entire application depends on this version. 如果您的3.5 DLL需要LINQ,它依赖于3.5(3.0准确)框架中的系统程序集,因此整个应用程序依赖于此版本。

You could load the 3.5 assembly dynamically and use reflection to get access to the functions you need. 您可以动态加载3.5程序集并使用反射来访问所需的功能。 This requires some overhead, of course. 当然,这需要一些开销。

Nothing pretty but there are ways to get the code happily working together (in the order of preference): 没有什么可以,但有一些方法可以让代码愉快地一起工作(按优先顺序):

1) Upgrade both projects to 3.5 1)将两个项目升级到3.5

If I understand you correctly then your .net FW 2.0 Program will have dependency on 3.5 Library, which means for every functionality of the Program to work, it now requires FW 3.5. 如果我理解正确,那么您的.net FW 2.0程序将依赖于3.5库,这意味着该程序的每个功能都可以使用,现在需要FW 3.5。 Since you state to have the code and authority to recompile the the Program AND install whatever FW on deployment, then you can upgrade it to 3.5. 由于您声明具有重新编译程序的代码和权限并在部署时安装任何FW,因此您可以将其升级到3.5。 Sounds simple, but since you did not do this, then I guess you have good reasons (like other programs being higher up the call chain which you cannot upgrade to 3.5/recompile.) 听起来很简单,但是既然你没有这样做,那么我猜你有充分的理由(就像其他程序在调用链中更高,你无法升级到3.5 /重新编译。)

2) Go around the FW2.0 compiler 2)绕过FW2.0编译器

Build the Program when referencing the 2.0 version of Library (or dummy, just providing the public API). 在引用2.0版本的库(或虚拟,仅提供公共API)时构建程序。 Build the 3.5 version of Library separately without Program (hence removing the need to reference the wrong FW assembly) and deploy the 3.5 version instead of the 2.0 version. 在没有程序的情况下单独构建3.5版本的库(因此无需引用错误的FW程序集)并部署3.5版本而不是2.0版本。 Since 2.0 and 3.5 use the same CLR runtime then fooling the compiler is enough. 由于2.0和3.5使用相同的CLR运行时,因此欺骗编译器就足够了。 As long as the deployment maching has FW 3.5 installed, everything should be fine. 只要部署机器安装了FW 3.5,一切都应该没问题。 Note: everything is fine even if you have just .net 2.0 present on deployment machine and the user does not call .net 3.5 classes. 注意:即使您在部署计算机上只有.net 2.0并且用户没有调用.net 3.5类,一切都很好。 If he does, there will be crash ;) 如果他这样做,就会崩溃;)

3) downgrade Library to 2.0 3)将Library降级到2.0

if you use only some classes of the .net FW then you could remain using the 2.0 compiler by adding those missing future assemblies to project. 如果你只使用.net FW的某些类,那么你可以通过将那些缺少的未来程序集添加到项目中来继续使用2.0编译器。 (this is the solution from Hanselman link shared by Craig). (这是Craig分享的Hanselman链接的解决方案)。 As already noted, you'll lose 3.5 compiler's syntactic sugar like vars. 如前所述,你将失去3.5版本的编译器的语法糖。

Choose whichever suits your situation best. 选择最适合您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM