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?
views:
48answers:
2
+1
Q:
What is the easiest way of detecting whether assembly targets ordinary .Net, CF or Silverlight?
+1
A:
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.
JaredPar
2010-02-01 17:11:59
+3
A:
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;
}
}
Bryan Batchelder
2010-02-01 17:26:36