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.
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.
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.
From command line
DUMPBIN your dll/exe /CLRHEADER
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.
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.
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
object[] list = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(true);
System.Runtime.Versioning.TargetFrameworkAttribute a = (System.Runtime.Versioning.TargetFrameworkAttribute)
list.Where(p => p.GetType() == typeof(System.Runtime.Versioning.TargetFrameworkAttribute)).FirstOrDefault();
Console.WriteLine(a.FrameworkName);
Console.WriteLine(a.FrameworkDisplayName);
Will return
a.FrameworkName ".NETFramework,Version=v4.0" string
a.FrameworkDisplayName ".NET Framework 4" string
a.FrameworkDisplayName ".NET Framework 4.5" string
a.FrameworkName ".NETFramework,Version=v4.5" string
a.FrameworkDisplayName ".NET Framework 4.5.1" string
a.FrameworkName ".NETFramework,Version=v4.5.1" string
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.
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.
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.
From command line
DUMPBIN your dll/exe /CLRHEADER
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.
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.
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
object[] list = System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(true);
System.Runtime.Versioning.TargetFrameworkAttribute a = (System.Runtime.Versioning.TargetFrameworkAttribute)
list.Where(p => p.GetType() == typeof(System.Runtime.Versioning.TargetFrameworkAttribute)).FirstOrDefault();
Console.WriteLine(a.FrameworkName);
Console.WriteLine(a.FrameworkDisplayName);
Will return
a.FrameworkName ".NETFramework,Version=v4.0" string
a.FrameworkDisplayName ".NET Framework 4" string
a.FrameworkDisplayName ".NET Framework 4.5" string
a.FrameworkName ".NETFramework,Version=v4.5" string
a.FrameworkDisplayName ".NET Framework 4.5.1" string
a.FrameworkName ".NETFramework,Version=v4.5.1" string
0 commentaires:
Enregistrer un commentaire