我可以使用.NET 4.0中的一个.NET 2.0应用程序库? [英] Can I use a .NET 4.0 library in a .NET 2.0 application?

查看:190
本文介绍了我可以使用.NET 4.0中的一个.NET 2.0应用程序库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些问题,用我的.NET 4.0库在.NET 2.0应用程序。我想我是在IM pression,作为一个Windows动态链接库,我的其他.NET应用程序将能够访问它。这是不是这样的?在两种环境中支持的应用程序有任何建议?

I'm running into some problems using my .NET 4.0 libraries in .NET 2.0 applications. I guess I was under the impression that being a Windows DLL, my other .NET apps would be able to access it. Is this not the case? Any recommendations in terms of supporting applications in both environments?

修改:我知道,我需要在目标系统上安装.NET 4.0框架,是否还有其他原因,这会不会/不应该工作

EDIT: I realize that I'd need to install the .NET 4.0 Framework on the target system, are there other reasons why this won't/shouldn't work?

修改:也许本来应该更具体。我们有一个当前,大型/复杂,应用程序编写.NET 2.0(ASP.NET是准确的)。我们拥有一套新的集成工具,我们书面方式在.NET 4.0中的工作流项目。我们想增加4.0创建的库之一到.NET 2.0的项目(目前在VS2008),并使用了一些它的方法。当我们这样做,我们遇到了问题,往往神秘的错误有关的记忆。

EDIT: Probably should have been more specific. We have a current, large/complex, application written in .NET 2.0 (ASP.NET be exact). We have a new set of integration tools and workflow items we are writting in .NET 4.0. We wanted to add one of the 4.0 created libraries to the .NET 2.0 project (currently in VS2008) and make use of a few of it's methods. When we do this we run into problems, often cryptic errors relating to memory.

这样看来,无论埃里克和布莱恩·拉斯穆森到底是正确的。由于我不是太热衷于通过COM暴​​露的东西(不知道这是技术上COM或没有,但不管)我想我会坚持的想法,两者是不兼容,并寻求其他途径,我认为我们根据我们的具体需要。从长期来看,我们将目光移到了.NET 2.0 code到4.0。

It would appear that both Earwicker and Brian Rasmussen are correct in the end. As I'm not too keen on exposing things via COM (not sure if this is technically COM or not but regardless) I think I will stick to the idea that the two are not 'compatible' and look to other means, which I think we have based on our specific needs. Longer term we will look to move the .NET 2.0 code up to 4.0.

推荐答案

是的,这是完全可能的。你只是揭露写在4.0的COM对象的组件。 2.0托管应用程序只使用他们作为COM对象和不知道他们是否是本地人,2.0,4.0或什么的。 COM是通用接口,这两个运行时版本必须实现相同。

Yes, this is perfectly possible. You just expose the components written in 4.0 as COM objects. The 2.0 hosting application just uses them as COM objects and has no idea whether they are native, 2.0, 4.0 or whatever. COM is the common interface that both runtime versions have to implement identically.

新的进程的SxS支持4.0意味着4.0的COM对象被加载时,它拉的所需运行,而不是试图在2.0上运行,因此两种运行时是present的过程中管理自己的对象。虽然无法直接通过他们之间的CLR对象,你可以通过COM接口,和CLR透明包装的COM接口的对象为你。

The new in-process SxS support in 4.0 means that when the 4.0-based COM object is loaded, it pulls in the required runtime instead of trying to run on the 2.0, so both runtimes are present in the process managing their own objects. Although you can't directly pass CLR objects between them, you can pass COM interfaces, and the CLR transparently wraps your objects in COM interfaces for you.

我不知道,如果你可以做一个单一的互操作程序集两个版本的工作。但很明显,你可以写在C#中的互操作程序集在2.0,将其导出到.​​TLB,然后将其导入到程序集中在4.0。这就给了你两个匹配互操作程序集描述相同的COM接口。 (或者只是建立相同的C#源在每个版本的安装工程)。

I don't know if you can make a single interop assembly for both versions to work from. But clearly you could write an interop assembly in C# in 2.0, export it to a .tlb, and then import it into an assembly in 4.0. That gives you two matching interop assemblies describing identical COM interfaces. (Or just build the same C# source in each version's assembly project).

奖励更新:将生成的应用程序将基于COM(带这就需要什么问题)

Bonus Update: Will the resulting application be COM-based (with whatever problems that entails)?

这要看你如何看待它。组件的作者将使其作为COM组件。因此主机应用程序需要找到并加载它们的COM组件。这意味着,打打闹闹与GAC,注册表或的SxS体现,这是一个很大不仅仅是告诉成分作者砸在某个目录中的组件,因此您可以与反思加载它不干净。

It depends how you look at it. Authors of components will be making them as COM components. So the host application needs to locate and load them as COM components. This means fooling around with the GAC, the registry or SxS manifests, which is a lot less clean than just telling component authors to drop their assembly in a certain directory so you can load it with reflection.

和它在运行时产生影响:当主机引用了一个组件,就会出现不是一个而是的对象参与。主持人引用了一个RCW,它的指针指向一个特定常规武器公约,则持有的引用,实际的组件实现的COM接口。中间的CCW是引用计数的COM对象,与主机的RCW有一个终结,调用发布的CCW,当它被摧毁这将释放 GCRoot 是保活的实际组成部分。

And it has an impact at runtime: when the host has a reference to a component, there will be not one but three objects involved. The host has a reference to a RCW, which has a pointer to a COM interface implemented by a CCW, which in turn holds a reference to the actual component. The CCW in the middle is a reference-counted COM object, and the host's RCW has a finalizer that calls Release on the CCW, and when it is destroyed it deallocates the GCRoot that is keeping alive the actual component.

这意味着 - 有回调指针等的足够复杂的安排 - 系统可能最终与循环引用计数的问题,其中一个对象断开连接的孤岛都持有到对方,所以他们从来没有得到释放。

This means that - with a sufficiently complicated arrangement of callback pointers, etc. - the system may end up with circular reference counting problems, where a disconnected "island" of objects are all holding references to each other and so they never get deallocated.

这篇关于我可以使用.NET 4.0中的一个.NET 2.0应用程序库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆