我已经在SO上找到this question,但是它仅涵盖CF,所以我的问题是:如何检测是否已为普通.Net,CF或Silverlight构建了程序集?

最佳答案

使用与链接问题的答案相同的方法:

public enum AssemblyType
{
    CompactFramework,
    Silverlight,
    FullFramework,
    NativeBinary
}

public AssemblyType GetAssemblyType(string pathToAssembly)
{
    try
    {
        Assembly asm = Assembly.LoadFrom(pathToAssembly);
        var mscorlib = asm.GetReferencedAssemblies().FirstOrDefault(a => string.Compare(a.Name, "mscorlib", true) == 0);
        ulong token = BitConverter.ToUInt64(mscorlib.GetPublicKeyToken(), 0);

        switch (token)
        {
            case 0xac22333d05b89d96:
                return AssemblyType.CompactFramework;
            case 0x89e03419565c7ab7:
                return AssemblyType.FullFramework;
            case 0x8e79a7bed785ec7c:
                return AssemblyType.Silverlight;
            default:
                throw new NotSupportedException();
        }
    }
    catch (BadImageFormatException)
    {
        return AssemblyType.NativeBinary;
    }
}

10-08 15:05