tags:

views:

121

answers:

3

Hi there, is it possible to tell if an assembly has changed?

I have a standard project that generates an assembly called MyAssembly.dll.

In a separate project i read the assembly and generate a hash.

When i generate a hash for the assembly it is different each time i recompile. I have set the assembly version to be static, are there any other attributes that i need to change?

class Program
{
    static void Main(string[] args)
    {
        var array = File.ReadAllBytes(@"MyAssembly.dll");
        SHA256Managed algo = new SHA256Managed();
        var hash = algo.ComputeHash(array);

        Console.WriteLine(Convert.ToBase64String(hash));
    }
}

Thanks

Rohan

A: 

You could store the hash in a text file after it is generated, then check against it the next time you recompile.

Peter
thats i what i'm doing, even when there are no changes to the code a recomplie generates a new hash.
Rohan West
+1  A: 

Well, if it's an assembly you manage yourself I would suggest adding a versionnumber and auto-increase the version number each time you build that assembly.

Then you can check on versionnumber instead.

Stormenet
The version number is the right way to check if an assembly has changed, is compatible
Peter Gfader
It is, but it could be the dll is from an external source that doesn't maintain it's versionnumbers (shame on them!).
Stormenet
Incrementing the version each time a build takes place wont tell me if the code has changed. Want i really want to do is check after a recomplie if an assembly has changed. I dont want to do selective build or create custom build scripts to only build projects that have changed.
Rohan West
+2  A: 

You are probably going to need to use the version number attribute. A hash will not work because any time you recompile an assembly, it's going to be different -- even if the code didn't change at all. The reason is that every time you compile, the compiler embeds a guid into the assembly, and it puts the same guid into the corresponding .pdb file. The guid will change every time the assembly is compiled.

This is how the debugger matches an assembly to the correct version of its .pdb file (it's also why you have to always save the .pdb's on anything you release, and you cannot rely upon being able to generate a pdb to match an existing assembly)

JMarsch