Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 605
Andrey Shchekin Avatar asked Dec 06 '25 18:12

Andrey Shchekin


2 Answers

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;
    }
}
like image 85
ctacke Avatar answered Dec 08 '25 12:12

ctacke


I rather use CCI or Cecil to parse its metadata and check out which set of references it depends on.

http://ccimetadata.codeplex.com/

http://www.mono-project.com/Cecil

like image 36
Lex Li Avatar answered Dec 08 '25 14:12

Lex Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!