6

I have some build agents that is building .NET code for us through a TeamCity setup, and I'm beginning to wonder if despite the project settings, they're outputting .NET 4.5 built assemblies. My doubts come from the fact that I don't know if Windows 2012 Server came with .NET 4.5 or 4.0 out of the box and thus whether it ever has had only 4.0 or any of the 4.0 assemblies available.

How can I look at a assembly on disk and determine whether it has been built with .NET 4.0 or 4.5?

As is evident by this blog post by Marc Gravell, there are differences in how these assemblies are built, even though we may not actually use any .NET 4.5 assemblies / features specifically.

I tried using ILDASM on the assembly, but the metadata reference on a known .NET 4.5(.1) console application still says 4.0:

// Metadata version: v4.0.30319

I tried starting the application Windows 7 with only .NET 4.0 installed, and it starts, but if the only way to figure out if everything works is by testing everything, then I'd rather try a bit more to see if it is built with the right version first.

So is this possible?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

2 Answers2

17

Run ildasm.exe and look at the manifest. You'll see the TargetFrameworkAttribute:

.custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = 
( 01 00 1A 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B   // ....NETFramework
  2C 56 65 72 73 69 6F 6E 3D 76 34 2E 35 01 00 54   // ,Version=v4.5..T
  0E 14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C   // ..FrameworkDispl
  61 79 4E 61 6D 65 12 2E 4E 45 54 20 46 72 61 6D   // ayName..NET Fram
  65 77 6F 72 6B 20 34 2E 35 )                      // ework 4.5

The CLR uses this attribute to verify that the correct version is present on the machine, prompting the user to get it automatically installed if it is not.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Too fast for me! :D Added the link to MSDN I had. – leppie Jan 09 '14 at 09:15
  • Thanks, then it does produce 4.0 assemblies as it is configured now. One less worry :) – Lasse V. Karlsen Jan 09 '14 at 09:18
  • I also ran this line in [LINQPad](http://linqpad.net): `Assembly.LoadFrom(@"d:\dev\vs.net\app451\app451\bin\debug\app451.exe").GetCustomAttributes(typeof(TargetFrameworkAttribute)).Dump();` and it produced the assembly version. – Lasse V. Karlsen Jan 09 '14 at 09:54
  • TargetFramework doesn't tell you what version the DLL requires. It only tells you what was targeted. If you don't have the reference assemblies installed, you may believe that 4.0 targets are dependent on 4.0 when they are actually dependent on v.Whatever, where "Whatever" is the first .NET DLL found by msbuild. – EKW Sep 22 '14 at 17:01
-1

The metadata version v4.0.30319 is for .Net 4.0. See here:

Generation Version number

1.0 - 1.0.3705

1.1 - 1.1.4322.573

2.0 - 2.0.50727.42

3.0 - 3.0.4506.30

3.5 - 3.5.21022.8

4.0 - 4.0.30319.1

4.5 - 4.5.50709.17929

4.5.1 - 4.5.50938.18408

Community
  • 1
  • 1
Yogster
  • 884
  • 1
  • 7
  • 27
  • 2
    The metadata line at the top of the manifest in ildasm for a .NET 4.5.1 console application also reads 4.0.30319, hence my question. The answer by @HansPassant though gave me my answer. – Lasse V. Karlsen Jan 09 '14 at 09:19
  • The page you're referring to ("[here](http://en.wikipedia.org/wiki/.NET_Framework#History)"), does not say "Generation Version number" at all (even the [2014-01-09 version](https://en.wikipedia.org/w/index.php?title=.NET_Framework&oldid=588641927)). There are similar numbers (e.g. "4.5.50938.18408") in [ru](https://ru.wikipedia.org/w/index.php?title=.NET_Framework&oldid=105392104#%D0%92%D0%B5%D1%80%D1%81%D0%B8%D0%B8) and [ro](https://ro.wikipedia.org/w/index.php?title=.NET_Framework&oldid=12690869)) versions, but they are not saying that these values are related to metadata version. – Igor Apr 14 '20 at 13:05