从 .Net 程序集中检索目标框架版本和目标框架配置文件 [英] Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly

查看:46
本文介绍了从 .Net 程序集中检索目标框架版本和目标框架配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译 .Net 程序集时,有什么方法可以访问用于 TargetFrameworkVersion 和/或 TargetFrameworkProfile 的值吗?

Is there any way that I can access the values that were used for TargetFrameworkVersion and/or TargetFrameworkProfile when a .Net assembly was compiled?

我所说的值是包含在项目文件中的值

The values I'm talking about are the ones contained the project file

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OtherStuff />
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
    <OtherStuff />
  </PropertyGroup>
  <OtherStuff>
  </OtherStuff>
</Project>

基本上,我想知道编译程序集时框架的目标版本是什么,如果可能的话,还有目标框架配置文件.

Basically I'd like to find out what the Target Version of the Framework was when the assembly was compiled and if possible the Target Framework Profile as well.

而且我不是在谈论当前加载的 CLR 版本,Environment.Version 不是我想要的.

And I'm not talking about the currently loaded version of the CLR, Environment.Version isn't what I'm after.

理想情况下,解决方案将使用 System.Reflection,但如果我不得不求助于其他方法,我会.

Ideally the solution would use System.Reflection but if I have to resort to other methods I will.

推荐答案

如果您对编译程序集的 CLR 版本感到满意,您可以使用 Assembly.ImageRuntimeVersion 属性.根据 MSDN,该属性:

If you would be satisfied with the version of the CLR that compiled the assembly, you can use the Assembly.ImageRuntimeVersion property. According to MSDN, that property:

表示保存在包含清单的文件中的公共语言运行时 (CLR) 的版本.

representing the version of the common language runtime (CLR) saved in the file containing the manifest.

默认情况下,ImageRuntimeVersion 设置为用于构建程序集的 CLR 版本.但是,它可能已在编译时设置为另一个值.

By default, ImageRuntimeVersion is set to the version of the CLR used to build the assembly. However, it might have been set to another value at compile time.

当然,这不会为您提供 .NET Framework 的特定版本(例如:.NET Frameworks 2、3.0 和 3.5 都在 2.0 CLR 上).

Of course, that doesn't give you the specific version of the .NET Framework (for example: .NET Frameworks 2, 3.0 and 3.5 are all on the 2.0 CLR).

如果 CLR 版本不够用,您可以尝试根据它引用的程序集估计"(聪明地猜测)它必须是哪个版本.对于 .NET 1 和 4,CLR 版本应该足够了.但是,如果 CLR 版本是 2.0,您将不知道这意味着 2.0、3.0 还是 3.5,因此您可以尝试更多逻辑.例如,如果您看到程序集引用了 System.Core(使用 Assembly.GetReferencedAssemblies()) 然后你就会知道版本是 3.5,因为 System.Core 是 3.5 中的新版本.这并不完全可靠,因为有问题的程序集可能不使用程序集中的任何类型,因此您将无法捕捉到它.为了尝试捕获更多情况,您可以遍历所有引用的程序集并检查它们的版本号 - 可能过滤到仅以 System 开头的程序集以避免其他库的误报.如果您看到任何 System.* 程序集引用的版本为 3.5.x.x,那么您也可以确定它是为 3.5 构建的.

If the CLR version isn't sufficient, you could to try to 'estimate' (guess intelligently) what version it must be based on the assemblies it references. For .NET 1 and 4, the CLR version should be enough. However, if the CLR version was 2.0, you wouldn't know if that meant 2.0, 3.0, or 3.5 so you could try some more logic. For example, if you saw that the Assembly referenced System.Core (using Assembly.GetReferencedAssemblies()) then you would know that the version is 3.5 since System.Core was new in 3.5. That's not exactly rock-solid since the assembly in question might not use any types from the Assembly so you wouldn't be able to catch that. To try to catch more cases, you could loop through all the referenced assemblies and check their version numbers - maybe filtering to just assemblies that start with System to avoid false positives with other libraries. If you see any System.* assemblies referenced that have a version of 3.5.x.x, then you can also be pretty sure it was built for 3.5.

正如您所注意到的,我不相信 TargetFrameworkProfile 会越过 Visual Studio.但是,如果恰好有应用程序的 app.config 文件,则 Visual Studio 可能已将目标框架放在那里.例如,如果您将项目设置为使用 4.0 客户端配置文件,Visual Studio 将创建一个 app.config,如下所示:

As you've noticed, I don't believe the TargetFrameworkProfile escapes past Visual Studio. However, if there happens to be an app.config file for the application, Visual Studio may have put the target framework in there. For example, if you set project to use the 4.0 Client Profile, Visual Studio creates an app.config like this:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>
</configuration>

这篇关于从 .Net 程序集中检索目标框架版本和目标框架配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆