如何获得构建程序集的.NET版本(1.1,2.0,3.0等)?

时间:2022-12-26 06:09:38

I have an assembly. Is there a way to detect which version of .NET was used to build that assembly?

我有一个集会。有没有办法检测用于构建该程序集的.NET版本?

5 个解决方案

#1


2  

You could possibly use Assembly.ImageRuntimeVersion. According to the MSDN docs, by default this is set to the version of the CLR used to build the assembly. Though apparently it can be changed.

您可以使用Assembly.ImageRuntimeVersion。根据MSDN文档,默认情况下,它设置为用于构建程序集的CLR的版本。虽然显然它可以改变。

It is a string property, so you would have to do some string comparison on it.

它是一个字符串属性,因此您必须对其进行一些字符串比较。

#2


1  

I don't believe so. It's relatively hard to build a .NET 1.1 app with .NET 2.0, although it's not impossible - so if you checked the version of mscorlib the app referenced, you could get a "probably accurate" picture of whether it was built with 1.1 or 2.0+.

我不相信。使用.NET 2.0构建.NET 1.1应用程序相对比较困难,虽然这并非不可能 - 所以如果你检查了引用的应用程序的mscorlib版本,你可以得到一个“可能准确”的图片,它是用1.1还是2.0构建的+。

You'd also need to ask yourself what answer you wanted when using VS2008 but building against a target of .NET 2.0. Are you actually interested in which version of the compiler was used? There may be some characteristic differences between the output of different versions of the compiler. Extra features could give it away too - if the app was written in C# targeting .NET 2.0, you could find types which look like they were generated from anonymous types in the source code. Of course that relies on the language feature being used in the first place...

你还需要问自己在使用VS2008时想要的答案,但是要针对.NET 2.0的目标。您真的对使用哪个版本的编译器感兴趣吗?不同版本的编译器的输出之间可能存在一些特征差异。额外的功能也可以放弃它 - 如果应用程序是用C#编写的,目标是.NET 2.0,你可以找到类似于源代码中的匿名类型生成的类型。当然,这依赖于首先使用的语言功能......

Why do you need to know this, out of interest?

为什么你需要知道这个,出于兴趣?

#3


1  

For frameworks < 2.0 and framework 4.0, the version of the referenced mscorlib will give you the framework version. For frameworks 2.0 - 3.5, check for the presence of any reference to System.Core or one of the other 3.5+ assemblies, and PresentationCore or any other 3.0+ assemblies. If you don't have any of those, it should be targeting 2.0.

对于框架<2.0和框架4.0,引用的mscorlib的版本将为您提供框架版本。对于框架2.0 - 3.5,检查是否存在对System.Core或其他3.5+程序集以及PresentationCore或任何其他3.0+程序集的任何引用。如果你没有这些,它应该是2.0。

#4


0  

Part of the problem with your question is that .NET has done a pretty good job of not changing code as versions increase. By that I mean that the mscorlib will be the same in .NET 4.0 as it was in .NET 1.1.

您的问题的部分问题在于.NET在版本增加时不会更改代码方面做得非常好。我的意思是mscorlib在.NET 4.0中与.NET 1.1中的相同。

But I sort of agree with the implied question of Jon Skeet: Why do you need to know? Is it purely out of interest, because I would think that it shouldn't matter to you.

但我有点同意Jon Skeet的隐含问题:你为什么需要知道?这纯粹是出于兴趣,因为我认为这对你来说无关紧要。

#5


0  

You could maybe use System.Reflection to check version number of system referenced assemblies.

您可以使用System.Reflection来检查系统引用的程序集的版本号。

using System;
using System.Reflection;

class Module1
{

    public static void CheckReferencedAssemblies(string assemblyPath)
    {
         Assembly a = Assembly.Load(assemblyPath);

        foreach (AssemblyName an in a.GetReferencedAssemblies() )
        {
             // Check an.Version for System assemblies
        }
    }
}

Edit: I'm not sure that I've understood what you want.

编辑:我不确定我是否理解你想要的东西。

#1


2  

You could possibly use Assembly.ImageRuntimeVersion. According to the MSDN docs, by default this is set to the version of the CLR used to build the assembly. Though apparently it can be changed.

您可以使用Assembly.ImageRuntimeVersion。根据MSDN文档,默认情况下,它设置为用于构建程序集的CLR的版本。虽然显然它可以改变。

It is a string property, so you would have to do some string comparison on it.

它是一个字符串属性,因此您必须对其进行一些字符串比较。

#2


1  

I don't believe so. It's relatively hard to build a .NET 1.1 app with .NET 2.0, although it's not impossible - so if you checked the version of mscorlib the app referenced, you could get a "probably accurate" picture of whether it was built with 1.1 or 2.0+.

我不相信。使用.NET 2.0构建.NET 1.1应用程序相对比较困难,虽然这并非不可能 - 所以如果你检查了引用的应用程序的mscorlib版本,你可以得到一个“可能准确”的图片,它是用1.1还是2.0构建的+。

You'd also need to ask yourself what answer you wanted when using VS2008 but building against a target of .NET 2.0. Are you actually interested in which version of the compiler was used? There may be some characteristic differences between the output of different versions of the compiler. Extra features could give it away too - if the app was written in C# targeting .NET 2.0, you could find types which look like they were generated from anonymous types in the source code. Of course that relies on the language feature being used in the first place...

你还需要问自己在使用VS2008时想要的答案,但是要针对.NET 2.0的目标。您真的对使用哪个版本的编译器感兴趣吗?不同版本的编译器的输出之间可能存在一些特征差异。额外的功能也可以放弃它 - 如果应用程序是用C#编写的,目标是.NET 2.0,你可以找到类似于源代码中的匿名类型生成的类型。当然,这依赖于首先使用的语言功能......

Why do you need to know this, out of interest?

为什么你需要知道这个,出于兴趣?

#3


1  

For frameworks < 2.0 and framework 4.0, the version of the referenced mscorlib will give you the framework version. For frameworks 2.0 - 3.5, check for the presence of any reference to System.Core or one of the other 3.5+ assemblies, and PresentationCore or any other 3.0+ assemblies. If you don't have any of those, it should be targeting 2.0.

对于框架<2.0和框架4.0,引用的mscorlib的版本将为您提供框架版本。对于框架2.0 - 3.5,检查是否存在对System.Core或其他3.5+程序集以及PresentationCore或任何其他3.0+程序集的任何引用。如果你没有这些,它应该是2.0。

#4


0  

Part of the problem with your question is that .NET has done a pretty good job of not changing code as versions increase. By that I mean that the mscorlib will be the same in .NET 4.0 as it was in .NET 1.1.

您的问题的部分问题在于.NET在版本增加时不会更改代码方面做得非常好。我的意思是mscorlib在.NET 4.0中与.NET 1.1中的相同。

But I sort of agree with the implied question of Jon Skeet: Why do you need to know? Is it purely out of interest, because I would think that it shouldn't matter to you.

但我有点同意Jon Skeet的隐含问题:你为什么需要知道?这纯粹是出于兴趣,因为我认为这对你来说无关紧要。

#5


0  

You could maybe use System.Reflection to check version number of system referenced assemblies.

您可以使用System.Reflection来检查系统引用的程序集的版本号。

using System;
using System.Reflection;

class Module1
{

    public static void CheckReferencedAssemblies(string assemblyPath)
    {
         Assembly a = Assembly.Load(assemblyPath);

        foreach (AssemblyName an in a.GetReferencedAssemblies() )
        {
             // Check an.Version for System assemblies
        }
    }
}

Edit: I'm not sure that I've understood what you want.

编辑:我不确定我是否理解你想要的东西。