woensdag 17 augustus 2011

Get the targetframework for an assembly

This is going to be a short post but I thought it might be useful for someone that encountered the same question.  So we basically have a number of options to get our targetframework from our assembly.  I’ll list three of my favored ones here:

ILDASM

This is a tool to inspect the generated IL of our code.  The cool thing about this tool is that we can also take a look at our manifest and see what the required framework is.
First launch the VS command prompt and type in ildasm.

From here the tool will launch and you’ll be able to select an assembly.



Once an assembly is selected we can view the manifest and retrieve the target framework.


DotPeek


This is a no-brainer so I’m just going to provide you with a screenshot and a link to download this awesome tool.


From code

Last but certainly not least, we can load the assembly in code and get the TargetFrameworkAttribute from code.
var asm = Assembly.LoadFile(@"…");
var targetFrameworkAttributes = asm.GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), true);
if (targetFrameworkAttributes.Length > 0)
{
    var targetFrameworkAttribute = (TargetFrameworkAttribute)targetFrameworkAttributes.First();
    Console.WriteLine(targetFrameworkAttribute.FrameworkDisplayName);
}

I included a sample here.

Small note:  Make sure that you have your dependencies in the same folder or the assembly will fail to load.

That’s it, I hope it was of use to anyone :-)

1 opmerking:

  1. Just to save others the trouble or searching: TargetFrameworkAttribute is only available from .NET 4.0.

    BeantwoordenVerwijderen