Determine framework (CLR) version of assembly

后端 未结 12 2307
生来不讨喜
生来不讨喜 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 04:11

    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:

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

    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.

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

    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);
    
    0 讨论(0)
  • 2020-11-27 04:17

    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.

    0 讨论(0)
  • 2020-11-27 04:21
    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.

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

    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

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