Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute hash of class code at runtime (C#)?

Tags:

c#

reflection

Is it possible to compute the hash of a class at runtime in C# (presumably through reflection)? To be clear, I don't want to compute the hashcode of an instance of said class, I want to compute the hash of the class code itself (if a function in the class changes, I'd expect a different hash code to be computed). Ideally this would only be sensitive to changes in the object code (and not just a hash of the string representation of the code itself).

Thanks in advance for your help,

-- Breck

like image 232
Breck Fresen Avatar asked Mar 19 '10 20:03

Breck Fresen


1 Answers

If you have the Type you can use GetMethod to get an instance of MethodInfo, which in turn has a GetMethodBody that returns the IL for said method. You can then hash that.

I am curious. Why do you want to do this in the first place?

like image 187
Brian Rasmussen Avatar answered Oct 13 '22 14:10

Brian Rasmussen