简体   繁体   English

可在.NET Framework 3.5或4.0上运行的可执行文件

[英]Executable running on either .NET Framework 3.5 or 4.0

We have various versions of our base application out in the field: 我们在现场有各种版本的基础应用程序:

  • Version 1.0 comes bundled with .NET Framework 3.5 1.0版捆绑了.NET Framework 3.5
  • Version 2.0 comes bundled with .NET Framework 4.0 2.0版捆绑了.NET Framework 4.0

We distribute updates via an installer which embeds a .NET executable in them. 我们通过安装程序分发更新,安装程序在其中嵌入.NET可执行文件。 The PC that this installer runs on may have the following combinations of our base application: 运行此安装程序的PC可能具有以下基本应用程序的组合:

  • Both Version 1.0 and Version 2.0 (.NET 3.5 / .NET 4.0) 版本1.0和版本2.0(.NET 3.5 / .NET 4.0)
  • Version 1.0 only (.NET 3.5 only) 仅限1.0版(仅限.NET 3.5)
  • Version 2.0 only (.NET 4.0 only) 仅限2.0版(仅限.NET 4.0)

I need to ensure that the .NET executable embedded in the installer will run reliably. 我需要确保安装程序中嵌入的.NET可执行文件可靠运行。

The approaches I have found suggest adding the following to the app.config file: 我发现的方法建议将以下内容添加到app.config文件中:

<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
</startup>

A few questions: 几个问题:

  1. Will this mean that it will use .NET Framework 4.0 if it is present, otherwise resorting to .NET Framework 3.5 (or any previous frameworks that use the 2.0.50727 CLR)? 这是否意味着它将使用.NET Framework 4.0(如果存在),否则将使用.NET Framework 3.5(或任何以前使用2.0.50727 CLR的框架)? IOW, are these tags ordered by priority? IOW,这些标签按优先级排序吗?

  2. Does the version of the .NET Framework on the build machine have any run-time effect on the application, or does the hardcoding of the versions in the app.config file always take priority? 构建计算机上的.NET Framework版本是否对应用程序有任何运行时影响,或者app.config文件中的版本的硬编码是否始终优先?

  3. What happens if I added code which relied upon a feature specific to .NET 4.0? 如果我添加了依赖于.NET 4.0特有的功能的代码,会发生什么? Would including the 2.0.50727 version in the app.config (as shown above) result in a compile-time error, or a run-time error (only if .NET 4.0 wasn't installed)? app.config包含2.0.50727版本(如上所示)会导致编译时错误或运行时错误(仅当未安装.NET 4.0时)?

  1. Yes, they're ordered by priority. 是的,他们按优先顺序排列。 It's easiest to try that with a simple console app: 使用简单的控制台应用程序尝试最简单:

     using System; class Test { static void Main() { Console.WriteLine(Environment.Version); } } 

    Compile that with .NET 3.5, then run it with your current configuration, and it'll show 4.0 - then switch the lines and it'll show 2.0. 使用.NET 3.5编译它,然后使用当前配置运行它,它将显示4.0 - 然后切换行,它将显示2.0。 See this MSDN article for more details. 有关更多详细信息,请参阅此MSDN文章

  2. The version of the framework that you've targeted your app at is the important thing. 针对应用程序定位的框架版本非常重要。 If your config specifies that you support .NET 2, but you've targeted .NET 4, the application won't start. 如果您的配置指定您支持.NET 2,但您的目标是.NET 4,则该应用程序将无法启动。

  3. You won't have relied on a feature specific to .NET 4 without targeting .NET 4, in which case it won't run on .NET 2. 如果不针对.NET 4,您将不会依赖.NET 4特有的功能,在这种情况下,它不会在.NET 2上运行。

Point 3 isn't strictly accurate, in that there are things you can do which will differ based on which version of .NET is running. 第3点是不是严格准确,有事情可以做,这将是不同基于.NET哪个版本的运行。 For example, change the app to: 例如,将应用更改为:

using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        object o = new List<string>();
        Console.WriteLine(o is IEnumerable<object>);
    }
}

When running with .NET 2 that will print False (because IEnumerable<T> was invariant in .NET 2) but when running with .NET 4 it will print True because of generic covariance. 当使用.NET 2运行时将打印False(因为IEnumerable<T>在.NET 2中是不变的)但是当使用.NET 4运行时,由于通用的协方差,它将打印True。

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

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