tags:

views:

43

answers:

3

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 technical or political limitation.

+3  A: 

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.

Reed Copsey
Thanks for the reply! I agree completely that ideally we'd want to upgrade the app, and this particular one is certainly an edge case.
Bob Palmer
@Bob: Yeah, I'm working on one of those too (see my comment on Kragen's post..)
Reed Copsey
A: 

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.

Kragen
There are times when this is not possible. For example, I'm currently developing a plugin for a pre-.NET 4, 3rd party application, and am having to revert to COM interop to make it work.
Reed Copsey
@Reed - True, but "How would one use a .Net 4.0 component in a .Net 2.0 app" implies that they are the author of the .Net 2.0 application.
Kragen
A: 

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

Nick Martyshchenko