简体   繁体   English

为什么为相同代码和相同.net框架版本生成的.net dll不是二进制相同的?

[英]Why the .net dlls generated for the same code and same .net framework version are not binary identical?

I have a C# project which when compiled on different machines generate dlls which are not binary equal. 我有一个C#项目,当在不同的计算机上编译时,会生成不是二进制相等的dll。 So my question is why the generated dlls are different? 所以我的问题是为什么生成的dll不同? and is there some way to get exactly same dll generated on different machines? 并有某种方法可以在不同的计算机上生成完全相同的dll?

EDIT Here is what i am trying to do. 编辑这是我想要做的。 There are multiple client machines which get some code snippet from a server and compile it. 有多个客户端计算机可以从服务器获取一些代码片段并进行编译。 After the dll is compiled, it get used over and over again on the client. dll编译后,将在客户端上反复使用。 If I could have the same DLLs generated on all machines then I can easily check that the DLL is not tampered with on the client side using cryptographic hash. 如果我可以在所有计算机上生成相同的DLL,则可以轻松地使用加密哈希检查DLL是否在客户端被篡改。

For some reasons the code has to be compiled at the client machine. 由于某些原因,必须在客户端计算机上编译代码。 Therefore digital signing is not an option. 因此,数字签名不是一种选择。

You cannot expect binaries to be same for any file recreated - let alone assemblies. 您不能期望二进制文件对于任何重新创建的文件都是相同的-更不用说程序集了。 All document metadata including data created, date modified will be different. 所有文档元数据(包括创建的数据和修改的日期)将有所不同。

If you need to compare, you need to sign the assembly (strong naming) and compare the public key token plus assembly version. 如果需要比较,则需要对程序集进行签名(强命名)并比较公钥令牌和程序集版本。


UPDATE UPDATE

What you are doing is asking for trouble although I can understand sometimes this has to be done. 尽管我有时可以理解这是必须要做的,但您正在做的是自找麻烦。 If I were you, I would compile the binary on server and get the clients to download the binary and not the code. 如果您是我,我将在服务器上编译二进制文件,并让客户端下载二进制文件而不是代码。 At the end of the day, clients might not have the C# compiler on their machine. 最终,客户端的计算机上可能没有C#编译器。

To answer your question, if you insist on compiling on client, everytime you compile, create a local hash and store in registry, some file, etc and then compare that hash. 为了回答您的问题, 如果您坚持要在客户端上进行编译,则每次编译时,都要创建一个本地哈希值,并将其存储在注册表,某些文件等中,然后比较该哈希值。


UPDATE 2 更新2

C# compiler never guarantees that it is going to create the same binary. C#编译器从不保证会创建相同的二进制文件。 Many things are created at the compiled time for example naming anonymous functions, backing value for automatic properties, anonymous methods, internal GUIDs... all of these will be created at compile time and while compiler uses a naming convention which names end up usually the same, it is not guaranteed . 在编译时会创建很多东西,例如,命名匿名函数,自动属性的支持值,匿名方法,内部GUID ...所有这些都将在编译时创建,而编译器使用命名约定时,名称通常以同样, 不保证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM