Determine framework (CLR) version of assembly

后端 未结 12 2306
生来不讨喜
生来不讨喜 2020-11-27 03:46

From the command line (or by any means really), how can I determine which CLR version a .NET assembly requires?

I need to determine if an assembly requires 2.0 or 4

相关标签:
12条回答
  • 2020-11-27 03:56

    Here is a powershell one liner that will display the Target framework version for assemblies targeting v4 and up.

     Resolve-Path($args) | Select @{N='Assembly'; E={$_ }}, @{N='TargetFramework'; E={(([Reflection.Assembly]::ReflectionOnlyLoadFrom($_).GetCustomAttributesData() | Where-Object { $_.AttributeType -like "System.Runtime.Versioning.TargetFrameworkAttribute" })).NamedArguments.TypedValue}} | Format-Table
    

    use:

    C:\test\> show-targetfw.ps1 *.dll
    
    Assembly             TargetFramework
    --------             --------
    C:\test\a.dll        ".NET Framework 4.6.1"
    C:\test\b.dll        ".NET Framework 4.5.2"
    
    0 讨论(0)
  • 2020-11-27 03:56

    From command line

    DUMPBIN your dll/exe /CLRHEADER

    0 讨论(0)
  • 2020-11-27 04:03

    If you want to include result in a script, I recommend using the text output of ildasm.exe, and then grep "Version String" from the output.

    "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe" /text D:\LocalAssemblies\Toolfactory.Core.BaseTypes.dll /noil /headers | find "' Version String"
    

    Note I include a ' so the find command does not recognize "Version String Length"

    0 讨论(0)
  • 2020-11-27 04:06

    One clarification...

    The problem with all the mentioned methods is that they will return version 4.0 if assembly was compiled against .NET framework 4.0, 4.5 or 4.5.1.

    The way to figure out this version programmatically at runtime is using the System.Runtime.Versioning.TargetFrameworkAttribute for the given assembly, for example

    using System;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Versioning;
    
    ...    
    
    object[] list = Assembly.GetExecutingAssembly().GetCustomAttributes(true);
    var attribute = list.OfType<TargetFrameworkAttribute>().First();
    
    Console.WriteLine(attribute.FrameworkName);
    Console.WriteLine(attribute.FrameworkDisplayName);
    

    Will return

    a.FrameworkName ".NETFramework,Version=v4.0"    string
    a.FrameworkDisplayName  ".NET Framework 4"      string
    
    a.FrameworkName ".NETFramework,Version=v4.5"    string
    a.FrameworkDisplayName  ".NET Framework 4.5"    string
    
    a.FrameworkName ".NETFramework,Version=v4.5.1"  string
    a.FrameworkDisplayName  ".NET Framework 4.5.1"  string
    
    0 讨论(0)
  • 2020-11-27 04:07

    I'd suggest using ReflectionOnlyLoadFrom() insted of LoadFrom()

    It has an advantage that it can load x64 and ia64 assemblies when running on x86 machine, while LoadFrom() will fail to do that.

    Though it still won't load .Net 4.0 assemblies from a 2.0 powershell.

    0 讨论(0)
  • 2020-11-27 04:11

    I use ILSpy as a replacement for Reflector. If you open the assembly in ILSpy, you can see, for example:

    [assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]

    0 讨论(0)
提交回复
热议问题