In the programs we develop, sometimes we need to be sure if versions of the compiled code (.dll, .exe) we wrote are equivalent of each other on various environments.
To concretize the scenario, let's consider this:
In enterprise solutions, generally there are many environments prior to production environment. In my case, these environmets are:
-- DEV: Development.
-- UAT: User Acceptance Test.
-- PREPROD: Pre-Production.
-- PROD: Production. (LIVE)
At the source control side, each of them has corresponding branch. (In my case, I have branches in TFS).
After developer is finished with development phase, he performs merge operations on branches in order to make his code tested and running on target environment.
Sometimes, developer cannot be sure if the running code is the same as he merged. So he needs to check, if the running assembly (.dll, .exe) version is the same as he coded, or some problem occured while merging the branches.
To smooth away these kind of suspicion, contents of assemblies can be compared to check if they are equal.
In a "headlong" way, if such a comparison is performed in bitwise manner, the result of the comparison will be incorrect. Because, compiler may put some code-irrelevant details into the assembly at the moment of compilation, which should not affect the result of comparison. For example, CSharp compiler injects date and time of build into the dll. These kind of details does NOT affect the code running in dll.
The comparison program should compare equality of running code in assembly, and only this.
So, the only way to check: Inspecting MSIL (Microsoft Intermediate Language), or IL in short, in the assembly, which is the output of compiler and input of JIT compiler.
We can retrieve IL content of an assembly through ildasm.exe, Microsoft IL disassembler tool which is shipped with Visual Studio.
After IL content is written to a text file, the program we write can compare to generated IL's line by line. If any line is different, then we can say these two assemblies are different.
Some points to consider:
-- The IL output of ildasm.exe contains comment lines. These lines need to be removed before comparison.
-- The compiler may inject GUID attributes to some types. So, the user may need to have an option of ignoring Guid attributes.
Steps of comparison
-- Call ildasm.exe to generate IL content and redirect the output text file
-- When .exe terminates, read text file newly created.
-- While reading the text file, line by line, remove comments by Regexy (//.*)
-- Merge lines and remove guid attributes with regex.
-- Compare two strings. If they are equal, then IL contents in asssemblies are equal.
Code:
The ildasmPath is read from .config file in this example, which is set to: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe
You can dowlonad the solution here
No comments:
Post a Comment