1

Given a assembly how do I determine (in code) what version of Silverlight that assembly is compiled against?

So I want a method that does this

public static decimal GetSilverlightVersion(string assemblyPath)
{
   Magic goes here
}

and it should return 2.0, 3.0 or 4.0

Note: the executing code is .net 4 not Silverlight

Simon
  • 33,714
  • 21
  • 133
  • 202

1 Answers1

1

The compiler embeds the [TargetFramework] attribute into the assembly. You can read it back at runtime with reflection. Some sample code:

        var asm = System.Reflection.Assembly.GetExecutingAssembly();
        var attr = asm.GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), false)
            as System.Runtime.Versioning.TargetFrameworkAttribute[];
        if (attr.Length > 0) {
            label1.Content = attr[0].FrameworkDisplayName;
        }

Displayed value on my machine: "Silverlight 4".

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I have no clue what "targeted file" means, only assemblies have a dependency on a Silverlight version. A file can be anything. There are additional Assembly class methods that let you pick an assembly, choose one that you care about. – Hans Passant Nov 28 '10 at 22:49
  • Hans. Sorry. I actually tried to undo my downvote but i left it too long. It looks like your solution will work. I just need to confirm. – Simon Nov 28 '10 at 23:04