是否可以在新的应用程序域中加载针对不同 .NET 运行时版本的程序集?

发布于 2024-08-26 07:59:28 字数 1748 浏览 9 评论 0原文

我有一个基于 .NET 2 运行时的应用程序。我想添加一点对 .NET 4 的支持,但不想(在短期内)将整个应用程序(非常大)转换为目标 .NET 4。

我尝试了“明显”的方法创建一个应用程序 .config 文件,具有以下内容:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
</startup>

但我遇到了一些我注意到的问题 这里

我想到了创建一个单独的应用程序域。为了测试它,我创建了一个针对 .NET 2 的 WinForm 项目。然后,我创建了一个针对 .NET 4 的类库。在我的 WinForm 项目中,我添加了以下代码:

        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase = "path to .NET 4 assembly";
        setup.ConfigurationFile = System.Environment.CurrentDirectory + 
          "\\DotNet4AppDomain.exe.config";

        // Set up the Evidence
        Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
        Evidence evidence = new Evidence(baseEvidence);

        // Create the AppDomain      
        AppDomain dotNet4AppDomain = AppDomain.CreateDomain("DotNet4AppDomain", evidence, setup);
        try
        {
            Assembly doNet4Assembly = dotNet4AppDomain.Load(
               new AssemblyName("MyDotNet4Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=66f0dac1b575e793"));
            MessageBox.Show(doNet4Assembly.FullName);
        }
        finally
        {
            AppDomain.Unload(dotNet4AppDomain);
        }

我的 DotNet4AppDomain.exe.config 文件如下所示:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
</startup>

不幸的是,这个执行 dotNet4AppDomain.Load 时抛出 BadImageFormatException。我的代码中是否做错了什么,或者我正在尝试做的事情不起作用?

谢谢你!

I've an application that is based on .NET 2 runtime. I want to add a little bit of support for .NET 4 but don't want to (in the short term), convert the whole application (which is very large) to target .NET 4.

I tried the 'obvious' approach of creating an application .config file, having this:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
</startup>

but I ran into some problems that I noted here.

I got the idea of creating a separate app domain. To test it, I created a WinForm project targeting .NET 2. I then created a class library targeting .NET 4. In my WinForm project, I added the following code:

        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase = "path to .NET 4 assembly";
        setup.ConfigurationFile = System.Environment.CurrentDirectory + 
          "\\DotNet4AppDomain.exe.config";

        // Set up the Evidence
        Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
        Evidence evidence = new Evidence(baseEvidence);

        // Create the AppDomain      
        AppDomain dotNet4AppDomain = AppDomain.CreateDomain("DotNet4AppDomain", evidence, setup);
        try
        {
            Assembly doNet4Assembly = dotNet4AppDomain.Load(
               new AssemblyName("MyDotNet4Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=66f0dac1b575e793"));
            MessageBox.Show(doNet4Assembly.FullName);
        }
        finally
        {
            AppDomain.Unload(dotNet4AppDomain);
        }

My DotNet4AppDomain.exe.config file looks like this:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" />
</startup>

Unfortunately, this throws the BadImageFormatException when dotNet4AppDomain.Load is executed. Am I doing something wrong in my code, or is what I'm trying to do just not going to work?

Thank you!

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

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

发布评论

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

评论(2

弥繁 2024-09-02 07:59:28

您以 2.0 为目标,因此它是加载到内存中的那个...您要求它加载 4.0 图像...它无法工作,如果您想这样做,您需要旋转正确版本的新运行时实例。

唯一的方法可能是在进程中托管第二个 CLR,如 是否可以在 C 程序中托管 CLR? 这在 .Net 4.0 中成为可能。

You target the 2.0 so it is the one loaded in memory... they you ask it to load a 4.0 image... it can't work you need to spin a new Runtime instance of the correct version if you want to do that.

The only way to do that may be to Host a second CLR inside your process like explained in Is it possible to host the CLR in a C program? witch became possible with .Net 4.0.

谈下烟灰 2024-09-02 07:59:28

我倾向于怀疑 .NET 运行时的版本 2 对 .NET 4 没有任何线索或理解。根据你问题的声音和性质,你正在以相反的方式处理它......你是否尝试过编译并以 .NET 4 为目标来加载 .NET 2 运行时库...我认为不可能在同一个版本中混合不同版本的编译代码(一个用于 .NET 4,另一个用于 .NET 2)过程...

I would be inclined to suspect that the version 2 of the .NET runtime has no clue nor understanding of .NET 4. By the sound and nature of your question, you are dealing with it in the reverse....Have you tried compiling and targetting for .NET 4 to load the .NET 2 runtime library...I would not think it is possible to inter-mix different versions of compiled code (one for .NET 4 and the other for .NET 2) in the same process...

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