确定 dll 的 .NET Framework 版本

发布于 2024-09-14 02:36:32 字数 166 浏览 9 评论 0原文

我有一个旧的 dll,它是针对 .NET 框架编译并部署的。我不确定它是针对哪个版本的 .NET 框架进行编译的。我想知道如何确定该 dll 是针对哪个版本的 .NET 框架进行编译的?我无法信任源代码,因为我相信它已升级到 Visual Studio 2008 并更改为 .NET Framework 版本 3.5。

I have an old dll that was compiled against the .NET framework and deployed. I am not sure which version of the .NET framework it was compiled against. I am wondering how I can determine which version of the .NET framework this dll was compiled against? I cannot trust the source code because I believe it has been upgraded to Visual Studio 2008 and changed to .NET framework version 3.5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

许一世地老天荒 2024-09-21 02:36:32

在 PowerShell 中,您可以使用以下命令获取目标运行时:

$path = "C:\Some.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).ImageRuntimeVersion

我根据 Ben Griswold 的回答将其改编为 PowerShell。

如果您想知道 Visual Studio 中指定的目标框架版本,请使用:

$path = "C:\Some.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).CustomAttributes |
Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | 
Select-Object -ExpandProperty ConstructorArguments | 
Select-Object -ExpandProperty value

您应该得到类似的信息

.NETFramework,版本=v4.5.2

In PowerShell you can use the following to get the target runtime:

$path = "C:\Some.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).ImageRuntimeVersion

I adapted this to PowerShell from Ben Griswold's answer.

If you want to know the target framework version specified in Visual Studio, use:

$path = "C:\Some.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).CustomAttributes |
Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | 
Select-Object -ExpandProperty ConstructorArguments | 
Select-Object -ExpandProperty value

You should get something like

.NETFramework,Version=v4.5.2

没有心的人 2024-09-21 02:36:32

dotPeek 是一个很棒的(免费)工具来显示此信息。

如果您在使用 Reflector 时遇到一些问题,那么这是一个不错的选择。

在此处输入图像描述

dotPeek is a great (free) tool to show this information.

If you are having a few issues getting hold of Reflector then this is a good alternative.

enter image description here

才能让你更想念 2024-09-21 02:36:32

您可以使用 ILDASM...

ildasm.exe C:\foo.dll /metadata[=MDHEADER] /text /noil

并检查输出中的“元数据部分”。它会是这样的:

元数据部分:0x424a5342,版本:1.1,额外:0,版本长度:
12、版本:v4.0.30319

“版本”标签会告诉您.NET Framework 版本。在上面的例子中是 4.0.30319

You can use ILDASM...

ildasm.exe C:\foo.dll /metadata[=MDHEADER] /text /noil

and check for the 'Metadata section' in the output. It would be something like this:

Metadata section: 0x424a5342, version: 1.1, extra: 0, version len:
12, version: v4.0.30319

The 'version' tag will tell you the .NET Framework version. In the above example it is 4.0.30319

寄居人 2024-09-21 02:36:32

将其加载到 Reflector 并查看它引用了什么?

例如:

在此处输入图像描述

Load it into Reflector and see what it references?

for example:

enter image description here

意中人 2024-09-21 02:36:32

您有几个选择: 要以编程方式从托管代码中获取它,请使用 Assembly.ImageRuntimeVersion:

Dim a As Assembly = Reflection.Assembly.ReflectionOnlyLoadFrom("C:\path\assembly.dll")
Dim s As String = a.ImageRuntimeVersion

从命令行中,从 v2.0 开始,如果您双击“MANIFEST”并查找“,ildasm.exe 将显示它”元数据版本”。 确定映像的 CLR 版本

You have a few options: To get it programmatically, from managed code, use Assembly.ImageRuntimeVersion:

Dim a As Assembly = Reflection.Assembly.ReflectionOnlyLoadFrom("C:\path\assembly.dll")
Dim s As String = a.ImageRuntimeVersion

From the command line, starting in v2.0, ildasm.exe will show it if you double-click on "MANIFEST" and look for "Metadata version". Determining an Image’s CLR Version

罪#恶を代价 2024-09-21 02:36:32

使用 ILSpy http://ilspy.net/

开源、免费,绝对是一个选择,因为现在 Reflector 是付费的。

Use ILSpy http://ilspy.net/

open source, free, definitely an option since now reflector is paid.

乜一 2024-09-21 02:36:32

只是简单地

var tar = (TargetFrameworkAttribute)Assembly
          .LoadFrom("yoursAssembly.dll")
          .GetCustomAttributes(typeof(TargetFrameworkAttribute)).First();

Just simply

var tar = (TargetFrameworkAttribute)Assembly
          .LoadFrom("yoursAssembly.dll")
          .GetCustomAttributes(typeof(TargetFrameworkAttribute)).First();
深海夜未眠 2024-09-21 02:36:32

最简单的方法:只需在任何文本编辑器中打开 .dll。看最后一行之一:
输入图片此处描述

对于 .Net 8.0,字符串略有更新 .NETCoreApp,Version=v8.0

The simplest way: just open the .dll in any text editor. Look at one of the last lines:
enter image description here

For .Net 8.0 the string is slightly updated .NETCoreApp,Version=v8.0

予囚 2024-09-21 02:36:32

通过 Visual Studio 的另一个选项,将对 DLL 的引用添加到任何项目,然后右键单击新引用并单击“属性”,您可以在运行时版本中看到您要查找的内容:

在此处输入图像描述

Yet another option via Visual Studio, add a reference to the DLL to any project, then right-clicking on the new reference and click Properties, you can see what you are looking for in Runtime version:

enter image description here

天冷不及心凉 2024-09-21 02:36:32

使用 ILDASM 反编译它,然后查看正在引用的 mscorlib 版本(应该几乎位于顶部)。

Decompile it with ILDASM, and look at the version of mscorlib that is being referenced (should be pretty much right at the top).

等风来 2024-09-21 02:36:32

如果您有 JetBrains 的 dotPeek,则可以在 Assembly Explorer 中看到它。

你能看到吗这个截图?我不是:(

If you have dotPeek from JetBrains, you can see it in Assembly Explorer.

Can you see this screenshot? im not:(

笔芯 2024-09-21 02:36:32

我快速编写了这个 C# 控制台应用程序来执行此操作:

https://github.com/stuartjsmith/binarydetailer

只需传递一个目录作为参数,它就会尽力告诉您其中每个 dll 和 exe 的网络框架

I quickly wrote this C# console app to do this:

https://github.com/stuartjsmith/binarydetailer

Simply pass a directory as a parameter and it will do its best to tell you the net framework for each dll and exe in there

荭秂 2024-09-21 02:36:32

Detect It Easy”也称为 DiE,是一个用于确定文件类型的程序。适用于 .dll 文件或其他 (.exe) 文件。绝对免费用于商业和非商业用途。

输入图片此处描述

"Detect It Easy" also known as DiE is a program for determining types of files. Works with .dll files or other (.exe) files. Absolute free for commercial and non-commercial use.

enter image description here

感悟人生的甜 2024-09-21 02:36:32

使用“读取dll文本内容”的方式:

private static readonly Regex CompiledNetCoreRegex = new Regex(@".NETCoreApp,Version=v[0-9\.]+", RegexOptions.Compiled);
private static readonly Regex CompiledNetFrameworkRegex = new Regex(@".NETFramework,Version=v[0-9\.]+", RegexOptions.Compiled);

// You can define other methods, fields, classes and namespaces here
public string GetTargetFramework(FileInfo dll)
{
    string contents = File.ReadAllText(dll.FullName);
 
    Match match = CompiledNetCoreRegex.Match(contents);
    if (match.Success)
    {
        return match.Value;
    }

    match = CompiledNetFrameworkRegex.Match(contents);
    if (match.Success)
    {
        return match.Value;
    }

    return "unable to compute target framework";
}

Using the "read the text contents of the dll" approach:

private static readonly Regex CompiledNetCoreRegex = new Regex(@".NETCoreApp,Version=v[0-9\.]+", RegexOptions.Compiled);
private static readonly Regex CompiledNetFrameworkRegex = new Regex(@".NETFramework,Version=v[0-9\.]+", RegexOptions.Compiled);

// You can define other methods, fields, classes and namespaces here
public string GetTargetFramework(FileInfo dll)
{
    string contents = File.ReadAllText(dll.FullName);
 
    Match match = CompiledNetCoreRegex.Match(contents);
    if (match.Success)
    {
        return match.Value;
    }

    match = CompiledNetFrameworkRegex.Match(contents);
    if (match.Success)
    {
        return match.Value;
    }

    return "unable to compute target framework";
}
我只土不豪 2024-09-21 02:36:32

扩展此处的答案,如果存在依赖程序集,则可能会崩溃。如果您很幸运并且知道受抚养人在哪里(或者更幸运的是,在 GAC 中),那么这可能会有所帮助...

using System.Reflection;
using System.Runtime.Versioning;
// ...
{
    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
    var asm = System.Reflection.Assembly.LoadFrom(@"C:\Codez\My.dll");
    var targetFrameAttribute = asm.GetCustomAttributes(true).OfType<TargetFrameworkAttribute>().FirstOrDefault();
    targetFrameAttribute.Dump();
}

Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
    var name = args.Name;

    if (name.StartsWith("Depends"))
        return System.Reflection.Assembly.ReflectionOnlyLoadFrom(@"C:\Codez\Depends.dll");

    return System.Reflection.Assembly.ReflectionOnlyLoad(args.Name);
}

参考:https://weblog.west-wind.com/posts/2006/Dec/22/Reflection-on-Problem-Assemblies

Expanding on the answers here, this can blow up if there is a dependent assembly. If you're lucky and you know where the dependent is (or even luckier, it's in the GAC) then this may help ...

using System.Reflection;
using System.Runtime.Versioning;
// ...
{
    AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
    var asm = System.Reflection.Assembly.LoadFrom(@"C:\Codez\My.dll");
    var targetFrameAttribute = asm.GetCustomAttributes(true).OfType<TargetFrameworkAttribute>().FirstOrDefault();
    targetFrameAttribute.Dump();
}

Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
    var name = args.Name;

    if (name.StartsWith("Depends"))
        return System.Reflection.Assembly.ReflectionOnlyLoadFrom(@"C:\Codez\Depends.dll");

    return System.Reflection.Assembly.ReflectionOnlyLoad(args.Name);
}

Reference: https://weblog.west-wind.com/posts/2006/Dec/22/Reflection-on-Problem-Assemblies

过期情话 2024-09-21 02:36:32

如果您使用的是 Linux 并且有单声道,我认为您可以使用 ikdasm 代替 ildasm.exe 。

我不太明白这个,所以不知道是否准确。但我做了一个简单的测试,编译了一个dll库,只改变了。然后我在输出(bin)目录上运行这个:

ikdasm -assemblyref lib.dll

每个目标框架的结果都不同。

net8.0

AssemblyRef Table
1: Version=8.0.0.0
        Name=System.Runtime
        Flags=0x00000000
        Public Key:
0x00000000: B0 3F 5F 7F 11 D5 0A 3A

netstandard2.1

AssemblyRef Table
1: Version=2.1.0.0
        Name=netstandard
        Flags=0x00000000
        Public Key:
0x00000000: CC 7B 13 FF CD 2D DD 51

net4.8

AssemblyRef Table
1: Version=4.0.0.0
        Name=mscorlib
        Flags=0x00000000
        Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89

这个似乎只显示主要版本,所以:

ikdasm -customattr lib.dll | grep System.Runtime.Versioning.TargetFrameworkAttribute

5: Assembly: 1 instance void class [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::'.ctor'(System.String) [".NETFramework,Version=v4.8", {FrameworkDisplayName = ""}]

If you're on Linux and have mono, I think you can use ikdasm instead of ildasm.exe.

I don't really understand this, so I don't know if it's accurate. But I made a simple test compiling a dll library, changing only <TargetFramework>. Then I ran this on the output (bin) dir:

ikdasm -assemblyref lib.dll

The result is different for each target framework.

net8.0

AssemblyRef Table
1: Version=8.0.0.0
        Name=System.Runtime
        Flags=0x00000000
        Public Key:
0x00000000: B0 3F 5F 7F 11 D5 0A 3A

netstandard2.1

AssemblyRef Table
1: Version=2.1.0.0
        Name=netstandard
        Flags=0x00000000
        Public Key:
0x00000000: CC 7B 13 FF CD 2D DD 51

net4.8

AssemblyRef Table
1: Version=4.0.0.0
        Name=mscorlib
        Flags=0x00000000
        Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89

This one seems to show only major version, so:

ikdasm -customattr lib.dll | grep System.Runtime.Versioning.TargetFrameworkAttribute

5: Assembly: 1 instance void class [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::'.ctor'(System.String) [".NETFramework,Version=v4.8", {FrameworkDisplayName = ""}]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文