Determine framework (CLR) version of assembly

.NetAssembliesVersion

.Net Problem Overview


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.0 CLR version.

.Net Solutions


Solution 1 - .Net

ildasm.exe will show it if you double-click on "MANIFEST" and look for "Metadata version". By default, it's the version that the image was compiled against.

Solution 2 - .Net

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

Solution 3 - .Net

class Program {
  static void Main(string[] args) { 
      System.Console.WriteLine(
             System.Reflection.Assembly.LoadFrom(args[0]).ImageRuntimeVersion);
  }
}

Compile and run the above application under the latest .NET Framework (as an older CLR may be unable to load assemblies requiring a newer CLR) and run it passing the path to the assembly you want to check as the command line argument.

Solution 4 - .Net

Here's a PowerShell equivalent of the .NET code suggested in another answer. Using PowerShell means that you can skip a few steps like creating and compiling an assembly.

At a PowerShell prompt, run the following:

[System.Reflection.Assembly]::LoadFrom("C:\...\MyAssembly.dll").ImageRuntimeVersion

By default, PowerShell uses the .NET v2 runtime, so you'll get an exception for assemblies targetting v4. Stack Overflow question How can I run PowerShell with the .NET 4 runtime? details methods for changing that, if required.

Solution 5 - .Net

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"

Solution 6 - .Net

From command line

DUMPBIN your dll/exe /CLRHEADER

Solution 7 - .Net

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.

Solution 8 - .Net

As @mistika suggested, it is better to use ReflectionOnlyLoadFrom() rather than LoadFrom(). The downside of this is that calling GetCustomAttributes() on an assembly loaded with ReflectionOnlyLoadFrom() throws an exception. You need to call GetCustomAttributesData() instead:

var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
var customAttributes = assembly.GetCustomAttributesData();
var targetFramework = customAttributes.FirstOrDefault(attr => attr.AttributeType.Equals(typeof(TargetFrameworkAttribute)));

var frameworkName = string.Empty;
var frameworkDisplayName = string.Empty;
if (null != targetFramework)
{
    if(targetFramework.ConstructorArguments.Any())
    {
        // first argument is the name of the framework.
        frameworkName = (string)targetFramework.ConstructorArguments[0].Value;
    }

    // search for a named argument called "FrameworkDisplayName"
    var frameworkDisplayNameArg = targetFramework.NamedArguments.FirstOrDefault(arg => arg.MemberName.Equals("FrameworkDisplayName"));
    if (null != frameworkDisplayNameArg)
    {
        frameworkDisplayName = (string)frameworkDisplayNameArg.TypedValue.Value;
    }
}

Console.WriteLine("Framework Name: " + frameworkName);
Console.WriteLine("Framework Display Name: " + frameworkDisplayName);

Solution 9 - .Net

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")]

Solution 10 - .Net

A very nice tool is JustDecompile from Telerik. You can open assemblies and the tool is showing whether they are targeting 4.5, 4.5.1 or 4.6

Solution 11 - .Net

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"

Solution 12 - .Net

Try this Assembly Information executable to get the assembly version, which tells you CLR version it requires, and as well other information such as Compilation options, Target Processor and References:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKlaus Byskov PedersenView Question on Stackoverflow
Solution 1 - .NetDarin DimitrovView Answer on Stackoverflow
Solution 2 - .NetFernando Gonzalez SanchezView Answer on Stackoverflow
Solution 3 - .NetmmxView Answer on Stackoverflow
Solution 4 - .NetRobin MintoView Answer on Stackoverflow
Solution 5 - .NetAnders ForsgrenView Answer on Stackoverflow
Solution 6 - .NetTinkuView Answer on Stackoverflow
Solution 7 - .NetmistikaView Answer on Stackoverflow
Solution 8 - .NetAndyView Answer on Stackoverflow
Solution 9 - .Netxr280xrView Answer on Stackoverflow
Solution 10 - .NetxforfunView Answer on Stackoverflow
Solution 11 - .NetferariasView Answer on Stackoverflow
Solution 12 - .NetyantaqView Answer on Stackoverflow