如何确定编译程序集的Silverlight版本?

时间:2010-11-25 01:05:10

标签: .net silverlight assemblies

给定程序集如何确定(在代码中)编译程序集的Silverlight版本是什么?

所以我想要一个执行此操作的方法

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

它应该返回2.0,3.0或4.0

注意:执行代码是.net 4而不是Silverlight

1 个答案:

答案 0 :(得分:1)

编译器将[TargetFramework]属性嵌入到程序集中。您可以在运行时使用反射将其读回。一些示例代码:

        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;
        }

我的机器上显示的值:“Silverlight 4”。