我该如何使用.NET 4.0 code在C#项目,该项目是使用.NET Framework 3.5构建?我该、该项目、如何使用、项目

2023-09-03 07:50:15 作者:只当我的温柔喂了狗

我要调用的dll这是从C#项目应该使用.NET 3.5建成使用.NET 4.0构建的。我的意思是.NET 4.0依赖code应执行只有在我的机器已经.NET 4.0安装,否则就跳过这一部分。 这是可能的(任何解决方法)?如果是的话,那么如何才能实现?

I want to call dll which is built using .NET 4.0 from C# project which should be built using .NET 3.5. I mean .net 4.0 dependent code should executed only when my machine has .net 4.0 installed otherwise just skip that part. Is this possible ( any workaround) ? If yes,then how it can be achieved?

推荐答案

而不是增加了.NET 4.0 DLL到您的参考,把它包装成一个服务并调用服务来访问你的方法。该服务可以是一个HTTP / HTTPS服务(如MVC4 REST风格的WebAPI,WCF),或一个窗口服务看投递文件夹或SQL表。

Instead of adding the .NET 4.0 DLL to your references, wrap it into a service and call the service to access your methods. The service can be a HTTP/HTTPS service (such as MVC4 RESTful WebAPI, WCF), or a window service watching a drop folder or a SQL table.

假设你有一个Windows服务(项目A),它是使用.NET 3.5构建的。 项目A需要调用它驻留在类库(项目B),这是使用.NET 4.0中内置的方法。

Lets say you have a Windows Service (Project A) which is built using .NET 3.5. Project A needs to call a method which resides in a class library (Project B) which is built using .NET 4.0.

通过这个设置,可以不加引用从项目一期工程B(项目A --- x - >项目B)

With this setting you cannot add a reference to Project B from Project A (ProjectA ---x--> ProjectB)

另一种选择是:

升级项目A到.NET 4.0,然后添加引用项目B,并调用你的方法

查找方式,项目A和项目B无添加的直接引用可以沟通。当然,这是有代价的,但如果你没有办法,这是最好的方法。您可以通过两种方式做到这一点: Upgrade Project A to .NET 4.0 and then add reference to the Project B and call your method

Find a way that project A and Project B can communicate without adding a direct reference. Of course it comes with a cost but if you have no alternative it is the best way. You can do this in two ways:

2.1 HTTP API:我将与MVC4网络的API的http://www.$c$cproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP

2.1 HTTP API: I would go with MVC4 Web APIs http://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP

2.2数据库队列:项目A将序列并插入请求到一个共享数据库(如SQL)表,项目B会一直监视该表中的新行。对于每个新行,它挑选他们运行所需要的功能和结果存储在另一个表。

2.2 Database queue: Project A would serialize and insert the requests into a shared Database (e.g. SQL) table and Project B keeps watching for new rows in that table. For each new row, it picks them up runs the desired functionality and stores the results in another table.

2.3 Drop文件夹:项目A将序列并写入它的要求和参数到一个文件并将其存储在一个共享文件夹。在B项目一直在寻找新的文件,在共享文件夹,只要它看到的,读它,删除它,并将结果存储在共享文件夹。

2.3 Drop folder: Project A would serialize and write its requests and parameters into a file and store it in a shared folder. The project B keeps looking for new files in that shared folder and as soon as it sees one, reads it, deletes it, and stores the results in the shared folder.

免责声明:我提出的解决方案非常适用于多,其中相当大的和重要的逻辑驻留在.NET 4.0中的项目不能移植到.NET 3.5的小项目。如果你只需要运行4.0 code几行,你不想打扰大型的复杂性则可能是你需要重新考虑你要实现你的.NET 4的项目是什么。你可能想看看How引用.NET 4.0组件中的.NET 3.5的项目

Disclaimer: My proposed solution is well suited for more than small projects in which a fairly large and important logic resides in .NET 4.0 project which cannot be ported into .NET 3.5. If you only need to run few lines of 4.0 code and you dont want to bother with large complexity then probably you need to rethink what you want to achieve in your .NET 4 project. You may want to check out How to reference .NET 4.0 assembly within .NET 3.5 projects