如何计算程序集的哈希值,以便可以使用该哈希值来判断程序集中的代码是否已更改? [英] How to compute a hash of assembly, so that this hash could be used to tell if the code in the assembly has changed?

查看:175
本文介绍了如何计算程序集的哈希值,以便可以使用该哈希值来判断程序集中的代码是否已更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在于,与之前的编译结果相比,重建完全相同的代码会生成不同的程序集.

The problem is that rebuilding exactly the same code generates a different assembly when compared with the result of the previous build.

我为什么需要这个?我有一个T4模板,可以从给定的合同程序集生成某些源代码.该源代码即使已生成,也已签入VCS.这是因为合同组装相对不频繁地更改.但是,当确实发生更改时,只要不重新评估上述T4模板,我将使构建失败.

Why do I need this? I have a T4 template that generates certain source code from the given contract assembly. That source code is checked in into VCS, even though it is generated. This is because the contract assembly changes relatively infrequently. However, when it does change, I would like to fail the build as long as the aforementioned T4 template is not reevaluated.

我的计划是将合同程序集的哈希码植入生成的源文件中,例如:

My plan is to plant the hash code of the contract assembly in the generated source file, for example:

// 1B-D0-06-48-02-C2-C5-C5-48-37-AA-61-66-6B-6D-01

在构建包含模板的项目时,将运行一个msbuild任务.该任务将计算合同程序集哈希值,并将其与烘焙到生成的源代码中的哈希值进行比较.不平等意味着合同程序集发生了变化,我们需要重新运行模板.

There would be an msbuild task that runs when the project containing the template is built. That task is going to compute the contract assembly hash and compare it against the one baked into the generated source code. Inequality means the contract assembly has changed and we need to rerun the template.

存储程序集版本无济于事-在开发过程中是相同的.

Storing the assembly version does not help - it is the same during the development.

另一种解决方案是在运行时将模板评估为一个临时文件,然后将其复制到生成的源文件中(如果不同)-无需哈希.

Another solution would be to evaluate the template at run-time into a temporary file and copy it over the generated source file if it is different - no hash required.

但是,我想吸引开发者注意合同变更的事实.在这一点上,我希望开发人员手动重新评估模板.但是只有在需要的时候.

However, I want to attract dev's attention to the fact that the contracts changed. At this point I want the developer to reevaluate the template manually. But only when it is needed.

我想我可以写一个反映合同汇编的复杂代码来创建某种类型的规范表示.应该采用这种规范表示的哈希值.但是,这并非微不足道,我想看看是否可以避免.

I think I could write a complex code that reflects on the contract assembly to create some kind of a canonical representation of its types. Taking the hash of this canonical representation should be it. However, this is not trivial and I would like to see if it can be avoided.

推荐答案

我想我可以编写一个反映合同汇编的复杂代码,以创建某种类型的规范表示形式

I think I could write a complex code that reflects on the contract assembly to create some kind of a canonical representation of its types

实际上, ildasm 可以做到这一点.如果使用以下参数调用:

Actually, ildasm can do this. If called with following arguments:

ildasm /text /pubonly _dll_

它将打印以输出装配的公共界面

it will print to stdout the assembly's public interface

这篇关于如何计算程序集的哈希值,以便可以使用该哈希值来判断程序集中的代码是否已更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆