从.NET程序集检索目标框架版本和目标框架简介 [英] Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly

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

问题描述

有没有办法,我可以访问被用于TargetFrameworkVersion和/或TargetFrameworkProfile,当一个.NET程序集编译的价值观什么办法?

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,<一个的当前加载版本href="http://msdn.microsoft.com/en-us/library/system.environment.version.aspx">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 已编译的程序集,您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.imageruntimeversion.aspx"><$c$c>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:

再presenting公共语言运行库(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框架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程序(使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getreferencedassemblies.aspx"><$c$c>Assembly.GetReferencedAssemblies())那么你会知道的版本是3.5,因为 System.Core程序 3.5是新的。这不完全是坚如磐石的,因为有问题的组件可能不会从大会使用任何类型所以你将无法赶上那要尽量捕到更多的情况下,您可以通过循环的所有的引用的组件,并检查其版本号 - 也许过滤,只组件与系统启动,以避免引用与其他库误报。如果你看到任何系统。*程序集有一个版本的3.5.xx,那么你也可以pretty的确认它是专为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天全站免登陆