.netreflectioncompact-frameworkassembliesmono.cecil

Detect whether the assembly was built for .NET Compact Framework


Having a .NET assembly, how can I detect whether it was built for .NET CF or a full framework?


Solution

  • It's quite simple:

    public enum AssemblyType
    {
        CompactFramework,
        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;
                default:
                    throw new NotSupportedException();
            }
        }
        catch (BadImageFormatException)
        {
            return AssemblyType.NativeBinary;
        }
    }