简体   繁体   中英

How do I compare two AssemblyName instances?

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()' results 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.

我会比较AssemblyName.FullName属性,因为它表示程序集的实际版本特定标识。

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

https://docs.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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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