开发者

What is the easiest way of detecting whether assembly targets ordinary .Net, CF or Silverlight?

开发者 https://www.devze.com 2022-12-18 12:57 出处:网络
I found this question already on SO, but it only covers CF, so my question i开发者_开发知识库s: how do I detect whether an assembly has been built for ordinary .Net, CF or Silverlight?Using the same a

I found this question already on SO, but it only covers CF, so my question i开发者_开发知识库s: how do I detect whether an assembly has been built for ordinary .Net, CF or Silverlight?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号