.NET 如何知道 dll 是相同的?

发布于 2024-09-19 19:49:44 字数 996 浏览 3 评论 0原文

我写了一些这样的示例代码,所有输出都是相同的,这对我来说很奇怪,

我想如果 assembly1 与 assembly2 相同是有意义的,因为程序集信息是相同的(例如名称、程序集版本、GUID)等)

但是,我更改了程序集信息并重新编译了 SampleCodedFormula.dll(然后将其重命名为 Changed-assemply-info-and-recompile-SampleCodedFormula.dll),令人惊讶的是, assembly3 输出仍然与 assembly1 相同。

assembly1,2,3 是从相同的代码库编译的。

谁能告诉我为什么会有这样的行为?这对你来说有意义吗?

var domain = AppDomain.CurrentDomain;
var assembly1 = domain.Load(AssemblyName.GetAssemblyName("c:\\SampleCodedFormula.dll"));
Console.WriteLine(assembly1.CodeBase);
Console.WriteLine(assembly1.GetExportedTypes()[0]);

var assembly2 = domain.Load(AssemblyName.GetAssemblyName("c:\\Copied-From-SampleCodedFormula.dll"));
Console.WriteLine(assembly2.CodeBase);
Console.WriteLine(assembly2.GetExportedTypes()[0]);


var assembly3 = domain.Load(AssemblyName.GetAssemblyName("c:\\Changed-assemply-info-and-recompile-SampleCodedFormula.dll"));
Console.WriteLine(assembly3.CodeBase);
Console.WriteLine(assembly3.GetExportedTypes()[0]);

I wrote some sample code like this, all the outputs are the same, which is weird for me,

I guess it makes sense if assembly1 is as same as assemly2, since the assembly information is the same(such as name, assembly version, GUID and etc.)

however, I changed the assembly information and recompile the SampleCodedFormula.dll(then rename it to Changed-assemply-info-and-recompile-SampleCodedFormula.dll), surprisingly, the assembly3 output is still same as assembly1.

assembly1,2,3 are compiled from the same codebase.

Can anyone tell me why the behavior is like this? does it make sense to you?

var domain = AppDomain.CurrentDomain;
var assembly1 = domain.Load(AssemblyName.GetAssemblyName("c:\\SampleCodedFormula.dll"));
Console.WriteLine(assembly1.CodeBase);
Console.WriteLine(assembly1.GetExportedTypes()[0]);

var assembly2 = domain.Load(AssemblyName.GetAssemblyName("c:\\Copied-From-SampleCodedFormula.dll"));
Console.WriteLine(assembly2.CodeBase);
Console.WriteLine(assembly2.GetExportedTypes()[0]);


var assembly3 = domain.Load(AssemblyName.GetAssemblyName("c:\\Changed-assemply-info-and-recompile-SampleCodedFormula.dll"));
Console.WriteLine(assembly3.CodeBase);
Console.WriteLine(assembly3.GetExportedTypes()[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-09-26 19:49:54

只需像这样检查程序集的版本

 // get the version object for this assembly
        Version v = System.Reflection.Assembly.GetExecutingAssembly().
         GetName().Version;

使用 FileVersionInfo 对象即可。以下是 Microsoft 网站上的示例,该示例从 notepad.exe 获取版本信息

public void GetFileVersion() {
    // Get the file version for the notepad.
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo("%systemroot%\\Notepad.exe");

    // Print the file name and version number.
    textBox1.Text = "File: " + myFileVersionInfo.FileDescription + '\n' +
       "Version number: " + myFileVersionInfo.FileVersion;
 }

Just by checking verstion of assembly like this

 // get the version object for this assembly
        Version v = System.Reflection.Assembly.GetExecutingAssembly().
         GetName().Version;

or

Use the FileVersionInfo object. Here's an example from the Microsoft website that gets the version info from notepad.exe

public void GetFileVersion() {
    // Get the file version for the notepad.
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo("%systemroot%\\Notepad.exe");

    // Print the file name and version number.
    textBox1.Text = "File: " + myFileVersionInfo.FileDescription + '\n' +
       "Version number: " + myFileVersionInfo.FileVersion;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文