c# - 我的程序集实际运行的是哪个版本的 .Net 框架?

标签 c# .net .net-3.5 clr .net-4.6

只需一点设置。我们安装了 .Net Framework 3.5、4.0、4.5 和 4.6.1。

如果我们使用 .Net Framework 3.5 构建一个 .Net 应用程序或程序集,然后我们将 app.config 设置为使用 4.6.1 支持的单个运行时运行:

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

实际使用的是哪个版本的框架?

这个问题来自阅读这篇文章:How do I force an application compiled to target .NET Framework 4 to run under .NET framework 4.6.1? . @Hans Passant 指出 TargetFrameworkAttribute 将决定框架的行为方式,他谈到存在导致特定代码运行的开关等。但是,我还没有找到任何可以解释哪个版本的核心 .Net 框架将在这种情况下运行。

对 .Net 框架的任何调用是否会使用 .Net 3.5 代码库、.Net 4.0 代码库(因为 4.0 clr 版本),或者是否会运行最新和最好的 .Net 4.6.1 代码库,因为这是使用 clr 4.0 安装的最新版本吗?

编辑:显示未具体说明运行时版本的 list 。

// Metadata version: v2.0.50727
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 2:0:0:0
}
.assembly ConsoleApplicationVersionTest
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.

  // --- The following custom attribute is added automatically, do not uncomment -------
  //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) 

  .custom instance void [mscorlib]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 1D 43 6F 6E 73 6F 6C 65 41 70 70 6C 69 63   // ...ConsoleApplic
                                                                                              61 74 69 6F 6E 56 65 72 73 69 6F 6E 54 65 73 74   // ationVersionTest
                                                                                              00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyDescriptionAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 1D 43 6F 6E 73 6F 6C 65 41 70 70 6C 69 63   // ...ConsoleApplic
                                                                                                61 74 69 6F 6E 56 65 72 73 69 6F 6E 54 65 73 74   // ationVersionTest
                                                                                                00 00 ) 
  .custom instance void [mscorlib]System.Reflection.AssemblyCopyrightAttribute::.ctor(string) = ( 01 00 12 43 6F 70 79 72 69 67 68 74 20 C2 A9 20   // ...Copyright .. 
                                                                                                  20 32 30 31 36 00 00 )                            //  2016..
  .custom instance void [mscorlib]System.Reflection.AssemblyTrademarkAttribute::.ctor(string) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.InteropServices.GuidAttribute::.ctor(string) = ( 01 00 24 34 36 36 31 33 34 32 33 2D 38 39 34 30   // ..$46613423-8940
                                                                                                  2D 34 39 36 65 2D 61 31 37 32 2D 37 36 36 31 31   // -496e-a172-76611
                                                                                                  64 30 66 31 32 32 38 00 00 )                      // d0f1228..
  .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )             // ...1.0.0.0..
  .hash algorithm 0x00008004
  .ver 1:0:0:0
}
.module ConsoleApplicationVersionTest.exe
// MVID: {11493526-C9AC-45F0-9784-D7712809998C}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x00000000010B0000

最佳答案

We have the .Net framework 3.5, 4.0, 4.5, and 4.6.1 installed

那是不准确的,这样的配置是不可能的。规则是您只能安装 一个 版本的 .NET 2.0 到 3.5,它针对 v2.0.50727 运行时。安装了 一个 .NET 4.0 到 4.6.2 版本,它面向 v4.0.30319 运行时。

因此您的机器可能安装了 4.0。当您安装 4.5 时,它覆盖 4.0 安装。没有留下任何痕迹。 4.5 非常有能力运行针对 4.0 的程序,微软花费了大量的精力来确保 4.5 向后兼容 4.0。同样,当您安装 4.6.1 时,它会覆盖 4.5

// Metadata version: v2.0.50727

您发布的 ildasm.exe 转储清楚地表明程序集针对 2.0.50727 运行时。如此简单,它将在 3.5 版本上运行。它将在 2.0 安装上运行得同样好,它不使用在 3.0 和 3.5 中添加的任何程序集。也许这是你的提示,Microsoft 在添加新程序集时会增加框架版本。只有当它创建一个新的运行时版本时,您才能进行另一个并行安装。这不太可能很快发生,不包括采用完全不同方法的 .NETCore。

关于c# - 我的程序集实际运行的是哪个版本的 .Net 框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752826/

相关文章:

c# - 使用对象类型在模块之间传递信息

c# - 固定一个函数指针

c# - 在 .NET 中添加斜杠/转义单引号

c# - 我的正则表达式做错了什么?

C# 和 SQL Server 2008 CLR 序列化问题

c# - ThreadPool 在短短几秒钟内使用了过多的内存

c# - 在 Windows Azure 上部署 WCF 服务

.NET:无法从 TA-Lib 获得正确的 RSI 和 MACD 值

c# - 将 Windows 窗体单元测试迁移到 WPF

.net-3.5 - 本地化 IEnumerable 的最佳方法是什么?