简体   繁体   English

在同一进程中将.NET 3.5与4 / 4.5程序集混合使用

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

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 APPLICATION to compile using .NET 4.5? 我想知道在我们使用.NET 4.5转换应用程序进行编译的情况下使用了什么运行时/核心库?

Should this scenario properly work? 这种情况应该适当吗? (loading .NET 3.5 assemblies in a 4.5 process)? (在4.5进程中加载​​.NET 3.5程序集)? * The plugin assemblies are loaded via reflection. *插件程序集通过反射加载。

How does the CLR runtime handle such a scenario? CLR运行时如何处理这种情况? is this a safe practice? 这是一种安全的做法吗?

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. 如果您将应用程序的主要EXE重新编译为目标.NET 4.x或使用带有<supportedRuntime>元素的app.exe.config文件来强制使用CLR版本4,那么您可以毫不费力地使用这两个.NET 3.5和.NET 4.0程序集。 CLR v4 has no trouble reading 3.5 assemblies, it is backwards compatible. CLR v4在读取3.5组件时没有问题,它向后兼容。 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. 不是相反,CLR v2无法读取版本4程序集,这就是为什么你需要.config文件,如果你的EXE不是针对v4。

The only wrinkle is the dependencies that your 3.5 assembly has on old framework assemblies. 唯一的缺点是3.5程序集对旧框架程序集的依赖关系。 It will for example ask for version 2.0.0.0 of mscorlib.dll. 例如,它会询问mscorlib.dll的2.0.0.0版。 The CLR automatically translates those requests and replaces them with version 4.0.0.0. CLR会自动转换这些请求,并将其替换为4.0.0.0版。 Which in general works just fine, the standard 4.0 framework assemblies are very compatible with the old versions. 一般情况下,标准的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. 然而,微软确实借此机会将4.0作为一个新的并排版本并修复了无法轻易修复的旧错误,而不会有破坏意外依赖于错误行为的代码的风险。 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. 但是,您必须重新测试您的代码以确保。

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 Library with 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 Application that references Net2Library.dll and has following class: Project'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,Culture = neutral,PublicKeyToken = b77a5c561934e089

You may also check out following resources: .NET Framework Assembly Unification Overview and Assembly Binding Redirection . 您还可以查看以下资源: .NET Framework程序集统一概述程序集绑定重定向

If you have a 3.5 assembly referenced from a 4.5 executable, both assemblies will run in the 4.5's CLR environment. 如果从4.5可执行文件引用3.5程序集,则两个程序集都将在4.5的CLR环境中运行。 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). 但是,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. 所以,至少根据我的经验,如果你想要针对2.0-3.5的程序集和其他目标4.0-4.5的程序集,你需要在客户端计算机上安装3.5和4.5框架版本。 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. 3.5完全向后兼容回2.0,因此你可以在一个环境中运行3.5,3.0和2.0。 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). 4.0-4.5与大多数旧代码兼容,但有一些重大变化(CAS是我最近偶然发现的一个案例),你必须明确地定位4.0(或设置一个SupportedRuntime app.config键)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM