C# 在同一进程中混合 .NET 3.5 和 4/4.5 程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13804219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:50:47  来源:igfitidea点击:

Mixing .NET 3.5 with 4/4.5 assemblies in the same process

c#.netclr

提问by lysergic-acid

I'd like to migrate a .NET 3.5 WinForms based application to the latest .NET version (4.5).

我想将基于 .NET 3.5 WinForms 的应用程序迁移到最新的 .NET 版本 (4.5)。

The application uses "external" components (can be thought of as plugins) that are also currently .NET 3.5 based.

该应用程序使用当前也基于 .NET 3.5 的“外部”组件(可以被认为是插件)。

I'd like to know what runtime/core libraries are used in case we convert ONLY THE APPLICATIONto compile using .NET 4.5?

我想知道在我们将ONLY THE APPLICATION转换为使用 .NET 4.5 进行编译的情况下使用了哪些运行时/核心库?

Should this scenario properly work? (loading .NET 3.5 assemblies in a 4.5 process)? * The plugin assemblies are loaded via reflection.

这种情况应该正常工作吗?(在 4.5 进程中加载​​ .NET 3.5 程序集)?* 插件程序集通过反射加载。

How does the CLR runtime handle such a scenario? is this a safe practice?

CLR 运行时如何处理这种情况?这是一种安全的做法吗?

回答by KeithS

If you have a 3.5 assembly referenced from a 4.5 executable, both assemblies will run in the 4.5's CLR environment. However, the 3.5 assembly will target the v3.5 libraries, not the v4.0 (although the 4.0 libraries will have all the same functionality as the 3.5, and more).

如果您有从 4.5 可执行文件引用的 3.5 程序集,则这两个程序集都将在 4.5 的 CLR 环境中运行。但是,3.5 程序集将针对 v3.5 库,而不是 v4.0(尽管 4.0 库将具有与 3.5 相同的所有功能,甚至更多)。

So, at least in my experience, if you want assemblies targeting 2.0-3.5 and other assemblies targeting 4.0-4.5, you will need both 3.5 and 4.5 framework versions installed on the client computer. 3.5 is fully backward-compatible back to 2.0, so you can have 3.5, 3.0 and 2.0 all running in one environment. 4.0-4.5 is compatible with most older code, but there are some breaking changes (CAS is one case I stumbled on recently), and you have to explicitly target 4.0 (or set up a SupportedRuntime app.config key).

因此,至少根据我的经验,如果您想要面向 2.0-3.5 的程序集和面向 4.0-4.5 的其他程序集,您将需要在客户端计算机上安装 3.5 和 4.5 框架版本。3.5 完全向后兼容回到 2.0,因此您可以在一个环境中运行 3.5、3.0 和 2.0。4.0-4.5 与大多数旧代码兼容,但有一些重大更改(CAS 是我最近偶然发现的一种情况),您必须明确针对 4.0(或设置 SupportedRuntime app.config 键)。

回答by Hans Passant

If you recompiled the main EXE of your app to target .NET 4.x or use an app.exe.config file with the <supportedRuntime>element to force CLR version 4 to get used then you'll have no trouble using both .NET 3.5 and .NET 4.0 assemblies. CLR v4 has no trouble reading 3.5 assemblies, it is backwards compatible. Not the other way around, CLR v2 can't read version 4 assemblies which is why you need the .config file if your EXE isn't targeting v4.

如果您重新编译了应用程序的主 EXE 以面向 .NET 4.x 或使用带有<supportedRuntime>元素的 app.exe.config 文件来强制使用 CLR 版本 4,那么使用 .NET 3.5 和 . NET 4.0 程序集。CLR v4 读取 3.5 程序集没有问题,它向后兼容。反之亦然,CLR v2 无法读取第 4 版程序集,这就是为什么如果您的 EXE 不是针对 v4 的,您需要 .config 文件。

The only wrinkle is the dependencies that your 3.5 assembly has on old framework assemblies. It will for example ask for version 2.0.0.0 of mscorlib.dll. The CLR automatically translatesthose requests and replaces them with version 4.0.0.0. Which in general works just fine, the standard 4.0 framework assemblies are very compatible with the old versions.

唯一的问题是 3.5 程序集对旧框架程序集的依赖。例如,它会要求 mscorlib.dll 的 2.0.0.0 版。CLR 会自动转换这些请求并将它们替换为 4.0.0.0 版。这通常工作得很好,标准 4.0 框架程序集与旧版本非常兼容。

Microsoft did however take the opportunity with 4.0 being a new side-by-side version and fixed old bugs that could not be easily fixed without risking breaking code that accidentally relied on the buggy behavior. They are very obscure bugs and it is pretty unlikely these bug fixes will byte you. You do however have to re-test your code to make sure.

然而,微软确实抓住了机会,将 4.0 作为一个新的并行版本,并修复了无法轻易修复的旧错误,而不会冒着破坏意外依赖于错误行为的代码的风险。它们是非常模糊的错误,这些错误修复不太可能会让您感到厌烦。但是,您必须重新测试您的代码以确保。

回答by Stipo

All assemblies will use types from .NET Framework which application targets.

所有程序集都将使用应用程序面向的 .NET Framework 中的类型。

Here is a simple test:

这是一个简单的测试:

Project 'Net2Library' which is a .NET Framework 2.0 Class Librarywith following class:

项目“ Net2Library”是一个.NET Framework 2.0 类库,具有以下类:

using System;
using System.Collections.Generic;

namespace Net2Library
{
    public class Class1
    {
        public static List<string> GetStrings()
        {
            var strings = new List<string>();
            Console.WriteLine("From Net2Library: {0}", strings.GetType().AssemblyQualifiedName);
            return strings;
        }
    }
}

Project 'Net4Application' which is a .NET Framework 4.0 Console Applicationthat references Net2Library.dlland has following class:

项目“ Net4Application”是一个.NET Framework 4.0 控制台应用程序,它引用 Net2Library.dll并具有以下类:

using System;
using Net2Library;

namespace Net4Application
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("From Net4Application: {0}", Class1.GetStrings().GetType().AssemblyQualifiedName);
        }
    }
}

Console output will be:

控制台输出将是:

From Net2Library: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 From Net4Application: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

来自 Net2Library: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken= b77a5c561934e089 来自 Net4Application:System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0.0.0 =b77a5c561934e089

You may also check out following resources: .NET Framework Assembly Unification Overviewand Assembly Binding Redirection.

您还可以查看以下资源: .NET Framework 程序集统一概述程序集绑定重定向