views:

808

answers:

5

I have an assembly. Is there a way to detect which version of .NET was used to build that assembly?

A: 

I don't believe so. It's relatively hard to build a .NET 1.1 app with .NET 2.0, although it's not impossible - so if you checked the version of mscorlib the app referenced, you could get a "probably accurate" picture of whether it was built with 1.1 or 2.0+.

You'd also need to ask yourself what answer you wanted when using VS2008 but building against a target of .NET 2.0. Are you actually interested in which version of the compiler was used? There may be some characteristic differences between the output of different versions of the compiler. Extra features could give it away too - if the app was written in C# targeting .NET 2.0, you could find types which look like they were generated from anonymous types in the source code. Of course that relies on the language feature being used in the first place...

Why do you need to know this, out of interest?

Jon Skeet
+2  A: 

You could possibly use Assembly.ImageRuntimeVersion. According to the MSDN docs, by default this is set to the version of the CLR used to build the assembly. Though apparently it can be changed.

It is a string property, so you would have to do some string comparison on it.

ckramer
So presumably this would distinguish between 1.1 and 2.0, but nothing more than that (until 4.0 comes out)?
Jon Skeet
That seems to be the case. I've got a recently built assembly here reporting "v2.0.50727"...That's with 3.5 SP1 no build number info.
ckramer
A: 

You could maybe use System.Reflection to check version number of system referenced assemblies.

using System;
using System.Reflection;

class Module1
{

public static void CheckReferencedAssemblies(string assemblyPath)
{
    Assembly a = Assembly.Load(assemblyPath);

    foreach (AssemblyName an in a.GetReferencedAssemblies() )
    {
        // Check an.Version for System assemblies
    }
}
}

Edit: I'm not sure that I've understood what you want.

madgnome
A: 

Part of the problem with your question is that .NET has done a pretty good job of not changing code as versions increase. By that I mean that the mscorlib will be the same in .NET 4.0 as it was in .NET 1.1.

But I sort of agree with the implied question of Jon Skeet: Why do you need to know? Is it purely out of interest, because I would think that it shouldn't matter to you.

Travis
+1  A: 

For frameworks < 2.0 and framework 4.0, the version of the referenced mscorlib will give you the framework version. For frameworks 2.0 - 3.5, check for the presence of any reference to System.Core or one of the other 3.5+ assemblies, and PresentationCore or any other 3.0+ assemblies. If you don't have any of those, it should be targeting 2.0.

280Z28