简体   繁体   English

如何获取C#.NET编译器程序集GUID

[英]How to get C# .NET Compiler Assembly GUID

I am trying to figure out when an assembly has changed (even when version number is the same). 我试图找出程序集何时更改(即使版本号相同)。

Best effort for now is finding out it has been recompiled, therefore would like to use the same item that MSBuild uses, which I believe is the files GUID stored in meta data? 目前,尽力而为是发现它已重新编译,因此想使用MSBuild使用的同一项目,我相信GUID文件存储在元数据中吗? (As per The .NET File Format ) 根据.NET文件格式

Is there any way I can access this info without re-inventing the wheel and dropping down to file bytes? 有什么方法可以访问此信息而无需重新发明轮子并放下文件字节?

Well, it seems that assemblies do not have associated GUIDs. 好吧,似乎程序集没有关联的GUID。 Modules, classes, methods, fields have GUIDs, and the "GUID" stream in a assembly is a place to store them. 模块,类,方法,字段具有GUID,而程序集中的“ GUID”流是用于存储它们的地方。

If you want to access meta infomations of a assembly (eg GUID of its main module), Mono.Cecil may be a good option. 如果要访问程序集的元信息(例如其主模块的GUID), Mono.Cecil可能是一个不错的选择。

But instead, you can achieve that by calucating a checksum using a hash function, or just taking a look at the modification date. 但是,相反,您可以通过使用哈希函数计算校验和或仅查看修改日期来实现。 System.IO.FilesystemWatcher is able to inform you, if you do not want to access the file intervally. 如果您不想间隔访问文件,则System.IO.FilesystemWatcher可以通知您。

Try to store in a variable last modified date of the assembly and check against it. 尝试将变量的最后修改日期存储在程序集中,并对照它进行检查。 Here is how to get last modified date of an assembly: 这是获取装配的上次修改日期的方法:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.FileInfo fileInfo = new System.IO.FileInfo(assembly.Location);
DateTime lastModified = fileInfo.LastWriteTime;

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

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