开发者

How do I compare two AssemblyName instances?

开发者 https://www.devze.com 2023-01-11 06:52 出处:网络
How do I compare two AssemblyName instances to figure out whether they represent the same assembly? The \'Equals\' method isn\'t overriden.

How do I compare two AssemblyName instances to figure out whether they represent the same assembly? The 'Equals' method isn't overriden.

One way I can think of is compare the 'ToString()' re开发者_如何学Pythonsults of both the instances but I would prefer an 'Equals' syntax since I need to prepare a list (List) of 'unique' AssemblyName instances and would like to use the 'Contains(AssemblyName item)' method.


As long as you are using strongly-named assemblies, comparing the output of the ToString method would be fine, as it outputs the full name of the assembly, which is supposed to be consistent and seems to be culture-invariant.

If the assemblies are not strong-named, then one can easily create another assembly with the same name and version number/culture and it would have the same assembly name as yours.

One thing to note though, because the assembly names are the same doesn't mean that the assemblies are the same physical identity; location is not part of the assembly name.


After some research, we have found this method: ReferenceMatchesDefinition (System.Reflection)

https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assemblyname.referencematchesdefinition?view=netframework-4.8

var expectedAssemblyName = new AssemblyName(name);
var actualAssemblyName = AssemblyName.GetAssemblyName(path);

if (AssemblyName.ReferenceMatchesDefinition(expectedAssemblyName, actualAssemblyName))
{
    // Some assemblies
}


I would compare the AssemblyName.FullName property, as it represents the actual version specific identity of the assembly.

0

精彩评论

暂无评论...
验证码 换一张
取 消