从 .NET 2.0 应用程序调用 .NET 4.0 WPF 类库

发布于 2024-10-30 22:39:05 字数 340 浏览 0 评论 0原文

我有一个用 .NET 4.0 编写的应用程序,我将其编译成 DLL 文件。我希望能够在 Windows 窗体 .NET 2.0 应用程序中使用此 DLL 文件。除了将几个字符串值传递给 4.0 DLL 之外,我不需要应用程序进行太多交互。 DLL 几乎是一堆我转换为 UserControls 的 Windows,我只需要能够引用它们来显示 2.0 应用程序中的 UserControls,而无需使用不同的 exe 并单独打包它们。最好的方法是什么?因为当我尝试将对 DLL 的引用添加到 2.0 应用程序时,它给了我一个错误消息,说 DLL 是在较新版本的 .NET 中构建的,所以我不能那样做。这是 COM 对象的用武之地吗?任何信息和链接我都会非常感激,谢谢。

I have an application written in .NET 4.0 that i compile into a DLL file. I want to be able to use this DLL file in a Windows Forms .NET 2.0 application. I don't need the application to really interact much, except just passing a couple string values to the 4.0 DLL. The DLL is pretty much a bunch of Windows which i converted to UserControls and i just need to be able to reference them to display the UserControls from the 2.0 application without having to use a different exe and having to package them separately. What is the best way to do this? Because when i try to add the reference to the DLL to the 2.0 application, it gives me and error saying the DLL is built in a newer version of .NET so i can't do it that way. Is this where a COM object would come in? Any information and links i would really appreciate, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-11-06 22:39:05

如果应用程序确实可以从 2.0 应用程序调用,那么最好的方法是将其编译为 2.0 应用程序。 Visual Studio 2010(和2008)支持多目标解决方案的概念。也就是说你可以用它来编译针对各种版本的 CLR 的项目。

我会采取两次编译应用程序的方法,

  • 一次用于 4.0,
  • 一次用于 2.0,

这样您就可以直接在 2.0 应用程序中使用 DLL。不需要凌乱的 COM 技巧

If the application can really and truly be called from a 2.0 application then the best approach is to compile it as a 2.0 application. Visual Studio 2010 (and 2008) support the notion of multi-targetted solutions. That is you can use it to compile a project for various versions of the CLR.

I would take the approach of compiling my application twice

  • Once for 4.0
  • Once for 2.0

This way you can use the DLL directly in your 2.0 application. No messy COM tricks needed

人间☆小暴躁 2024-11-06 22:39:05

从 .net 2.0 调用 .ne 4.0 确实是可能的。问题不在于这个,但我认为你不能发送用户控件!

我在几个灵魂中这样做,将简单的类从 4.0 传递到我的 2.0 程序集。诀窍是将 4.0 程序集注册为 ServicedComponent (COM+),然后在这些程序集之间共享一个接口 (.net 20)。然后在 .net 2.0 程序集中实例化 ServicedComponent 并从“中间件”程序集中检索接口的实例。

如前所述,您只能通过此方式传输接口,并且我认为将复杂类型编组为 UserControl 将非常困难。

Calling a .ne 4.0 from a .net 2.0 is indeed possible. The problem is not that, but i dont think you can send the usercontrols across!

Im doing this in a couple of soultions where im passing simple classes from 4.0 to my 2.0 assemblies. The trick is to register the 4.0 assembly as a ServicedComponent (COM+), then share an interface (.net 20) betwewen these assemblies. Then in your .net 2.0 assembly instantiate the ServicedComponent and retrieve an instance of your interface from the "middleware" assembly.

As stated, you can only transfer interfaces through this, and marshalling a complex type as a UserControl i think will be very difficult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文