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

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

问题描述

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

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.

我的计划是在生成的源文件中植入合约程序集的hash码,例如:

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天全站免登陆