开发者

Is there a way to marshal a .NET 4 library into a .NET 2 application?

I'm currently working on a few old apps written in a variety COM compliant languages, but which have a plugin architecture which only supports .NET2 plugins. W开发者_高级运维e've tried to upgrade to .NET4 and failed, so for now we're stuck with .NET2 as the CLR runtime in our app.

We now need to access some rather complex web services, for which we'd like to use WCF. Option number one is to use a COM wrapper and call the .NET4 libraries from native code, but we lose the use of our plugin mechanism. I'd like to know if there is a way to run .net 2 & 4 components in the same process, or some other way I could possibly solve this?


A slight change to @Kennet's approach is to just change the config file so that the app runs in .Net 4 without recompiling.

Here's an article I found on this subject: Running .NET 2 Runtime applications under the .NET 4 Runtime

Here's the important bits from the article (I have not tried this myself)...

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
  <NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>


Here's a horrible idea (it's such a bad idea that I'd fire me if I proposed it).

You might be able to force the app to run in the .Net 4 CLR with a loader app.

The loader app should be a .Net 4 executable that does nothing but load the app's executable assembly, finds the program entry point and executes it. Because .Net 2 assemblies are valid 4 assemblies, it should run no problem, but should then support .Net 4 plugins.

Update:

I ran a quick test version and it at least has the potential to work. Here's the source code for a loader program. My .Net 2.0 app was named "App20.exe" substitute your own name in there.

using System;
using System.Reflection;
using System.IO;

namespace Loader
{
    class Program
    {
        static void Main(string[] args)
        {
            string oldAppName = Path.GetFullPath("App20.exe");
            Assembly asm = Assembly.LoadFile(oldAppName);
            Console.WriteLine(asm.EntryPoint.Name);
            asm.EntryPoint.Invoke(null, new object[]{args});
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜