A file is an assembly if and only when if it’s pulled off and it comprises an Assembly entry in its CLR metadata.
Determining by hand
A degraded way to ascertain whether a file is an assembly is to campaign ildasm.exe on it. If it right away consecrates an error supposing that it may not be a PE file, and so it’s not a pulled off file. But, if it is an assembly, so ildasm will usher an entry for the Assembly definition (“.assembly“ in the MANIFEST window or at the bottom of the original window).
Finding out programmatically
From unmanaged code, you can ring GetAssemblyFromScope() on the IMetaDataAssemblyImport interface for the file. If it yields S_OK, it’s an assembly. If it renders CLDB_E_RECORD_NOTFOUND, it’s not an assembly.
From brought off code, if AssemblyName.GetAssemblyName(), Assembly.Load*(), etc. follows when paid that file, and so it’s an assembly. If the load failed with a BadImageFormatException, and then it may not be an assembly. There are other reasons, all the same, why that exception may have been held (peradventure it’s an assembly but could not be charged because it has an wrong format). Making out presently in v2, if the hresult for BadImageFormatException is COR_E_ASSEMBLYEXPECTED, and then it’s because it’s not an assembly. Arrest the exception and phone System.Runtime.InteropServices.Marshal.GetHRForException() to obtain its hresult to rule out.
Observe that this takes up that you are not concerned about performance. If you are concerned about that, so the way to optimize for it is to (more…)