Question

I found this question already on SO, but it only covers CF, so my question is: how do I detect whether an assembly has been built for ordinary .Net, CF or Silverlight?

Was it helpful?

Solution

Using the same approach as the answer to the linked question:

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

OTHER TIPS

I think the simplest way to do this is to see which version of mscorlib.dll a given assembly references. You can use the public key / version number of mscorlib to classify which version of the .Net framework it is as it's tied directly to the CLR. You can use the assemblies reference to mscorlib to get the version information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top