While l was looking over some questions about MEF, I stumbled onto this particular answer to a question. It made me wonder about such a bit since I’ve never had to attempt such but can see it being very valid in the scenario of that question.
Scenario:
If you have a directory of various .Net Assemblies all named different, how would you be able to identify ones that may be the same but renamed (i.e. Copy Of MyAssembly.dll vs MyAssembly.dll)?
I can think of the following items:
-
Check File Size (should be the same)
-
Check Assembly Version Number
-
Loop through the assembly using Reflection and attempt to locate any differences.
Is there any other/easier way of addressing this issue? Are there other criteria to look at for determining if 2 differently named DLLs are in fact the same compiled Assembly?
At first I thought you could use the
EqualsorReferenceEqualsto do this, but this proofs too error prone. If you useAssembly.LoadFile, this will not work, for instance.With nUnit, I did the following tests, which are a bit basic, but gives you something to go on. The weird way of loading the types is necessary (see MSDN). I assume you know how to do the “quick tests” in case you want to check for binary equality etc (see PS below).
A note on the code: this method of comparison will treat two assemblies as equal when they contain the same types. This means that a debug and a release build, or different versions, are not taken into consideration. Use
asm1.GetName()and its properties (again: do not use Equals!) to compare the individual strings (version, full name etc).PS: it would be intriguing to define what constitutes two equal assemblies, i.e.:
depending on what you choose, two entirely different assemblies (i.e. debug build vs release build) can come up as equal. It really depends on how you want to compare.
Update: corrected previous errors and added code sample