1

Is it discoverable, at runtime, which .NET Framework version was targeted by my own compile? The environment in which I am writing allows me to write C# that is compiled on another machine, but I do not know which language version is available, other than it is 2.0 CLR. I'd like to write a test that is output to the log file indicating the targeted framework.

For this to work, I'd guess the compiled assembly would have to carry that information.

This question is NOT how to list all frameworks on the runtime machine, as all versions to current are installed. This is not a duplicate of that question. This is also not a duplicate of how to determine the CLR with System.Environment.Version. I'm also not trying to detect at compile time the target .NET runtime. This needs to be a runtime detection so I can send it to the log.

Robert Kerr
  • 1,291
  • 2
  • 17
  • 34
  • 1
    look here - http://stackoverflow.com/questions/8517159/how-to-detect-at-runtime-that-net-version-4-5-currently-running-your-code – Matthew Layton Nov 08 '12 at 13:50
  • Seems like the problem is hardly solvable. Take a look at the above by @acrivwerx, it's useful. Some additional info, though - you can use Assembly.GetReferencedAssemblies to see which dlls (versions) your app references. The problem with that is that the apps targeting things above 2.0 are aren't guaranteed to use anything but the 2.0 dlls, since .NET frameworks before 4.0 just adds extra dlls to the existing 2.0 ones. – Greg Kramida Nov 08 '12 at 14:00
  • I'm a bit confused, do you mean you want to find out at runtime if the code was compiled to target v2.0 or v4.0 of the clr? – Davin Tryon Nov 08 '12 at 14:18
  • @activwerx, good link but pretty much covers all the topics except what is needed. the only time he came close he merely questioned why anyone might need to do so. well, i need to do so. – Robert Kerr Nov 08 '12 at 21:19
  • @dtryon yes, i'd like to know the targeted framework on the compile options, but by reading that at runtime from the compiled code, so the assembly would need to carry that in its manifest and read the value. – Robert Kerr Nov 08 '12 at 21:20
  • Do you have access to any of the msbuild files? like '.csproj'? – Maslow Nov 12 '12 at 15:29

2 Answers2

0

A decompiler will sometimes tell you that. For instance, Telerik's free justdecompile shows this, but not for all assemblies

TargetFrameworkPicture

zoomed a bit more here

enter image description here

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • That doesn't have anything to do with the decompiler, the attribute is specific to recent compilers. It won't appear for the compilers that shipped with 3.5 SP1 and earlier, not sure about 4.0, definitely for 4.5 – Hans Passant Nov 12 '12 at 16:57
0

This information is stored in the Assembly because it's necessary to run the EXE file. I read the Target Framework with this function:

    public string GetTargetFramework()
    {
        System.Reflection.Assembly assembly;
        Attribute[] attributes;
        System.Runtime.Versioning.TargetFrameworkAttribute targetFrameworkAttribute;

        assembly = Assembly.GetEntryAssembly();
        attributes = AssemblyDescriptionAttribute.GetCustomAttributes(assembly);
        foreach(Attribute item in attributes)
        {
            targetFrameworkAttribute = item as System.Runtime.Versioning.TargetFrameworkAttribute;
            if(targetFrameworkAttribute != null)
                return targetFrameworkAttribute.FrameworkDisplayName;
        }
        return null; // "TargetFrameworkAttribute not found"
    }

I intentionally explicitated the namespaces.