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
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"
From command line
DUMPBIN your dll/exe /CLRHEADER
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"
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
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.
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")]