How would one use a CLR 4.0 component in a CLR 1.1 or 2.0 App?
While I have seen some discussions on the utilization of an older CLR 2.0 component within a newer CLR 4.0 application, how would one handle the opposite case?
For example, if one had a legacy app in .Net 2.0, and wanted to take advantage of a newer business logic that took full advantage of the 4.0 version of the CLR, how would one go about referencing the newer component within the legacy application and accessing it's methods? And would the same method work if the legacy app was in .Net 1.1?
(Addendum For clarity)
While an upgrade is ideal, there still exist edge cases where this may be a tec开发者_如何学Gohnical or political limitation.
In general, you should upgrade the legacy application to use .NET 4. The code should not require changes.
Otherwise, the main supported method to handle this is via COM interop. Your .NET 4 class can be marked [ComVisible]
, and then you can construct interop assemblies to "consume" it in older .NET versions.
This, however, has many disadvantages - including a lot of added complexity and reduced performance.
You should target your older .Net 1.1 or 2.0 application at .Net 4.0 instead - as the target machine will need to have .Net 4.0 installed anyway in order to use the .Net 4.0 component there is no real advantage to targeting your application at .Net 2.0.
Take a look to Rick Byers and Simon Hall: CLR 4 - Side-by-Side In-Process - What. How. Why.
Check this comment:
Assumed i have the level of isolation you mentioned in the video, is it possible to load to an managed clr2 application the clr4 runtime In-process SxS (and vice versa)? If yes, what do i have to do? Is their any managed api i have to call? Or do i need the come layer or something which then loads the other clr for me? Are their any samples for the beta1 where i can have a look at?
CLR loading is really something that happens through native APIs, either using hosting APIs directly (eg. ICLRMetaHost), or indirectly via COM. You can do this in managed code (no need to actually write any native code) by using .NET's interop facilities. So yes, you could write code that ran in CLR v2 which loaded CLR v4 and vice-versa.
I know we've got some code samples in the works, but I don't think they've been posted yet. Keep an eye on the SxS category of the CLR team blog. The simplest way is to build a managed COM component using one version of .NET, and then instantiate it using COM-interop from another version of .NET exactly as you would without SxS. Look for any samples for building and using COM components with .NET 2.0.
I hope this helps, Rick
精彩评论