How does AssemblyName.ReferenceMatchesDefinition work?
Given the following code:
var n1 = new AssemblyName ("TestDll, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089");
var n2 = new AssemblyName ("TestDll, Version=2.0.0.2001, Culture=en-US, PublicKeyToken=ab7a5c561934e089");
Console.WriteLine (AssemblyName.ReferenceMatchesDefinition (n1, n2));
Console.WriteLine (AssemblyName.ReferenceMatchesDefinition (n2, n1));
Why do both of these checks print "True"? I would have thought that AssemblyName.ReferenceMatchesDefinition should consider differences in the version, culture, and public key token attributes of an assembly name, shouldn't they?
If not, what does ReferenceMatchesDefinition do that a comparison of the simple names doesn开发者_如何学编程't?
I've reported the issue on Microsoft Connect, and it has been confirmed a bug:
This is indeed a bug in the API. It has been in the product since it was introduced in 2.0 RTM. It never worked properly.
[...]
You may also consider API AppDomain.ApplyPolicy (with manual AssemblyName comparison). The API covers Framework assembly unification and binding redirects. You might also try to cover non-strong name assembly references. When PublicKeyToken is not present in the reference, only simple name match happens, the rest is ignored.
I think this blog post by Junfeng Zhang is relevant, especially the earlier blog post he links to about assembly identity. As usual with him, I don't understand any of it. Good luck!
simply checking the msdn would have solved the problem.
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.referencematchesdefinition.aspx
Precisely: "Returns a value indicating whether the loader resolves two assembly names to the same assembly."
Apperently, both AssemblyNames ultimately resolve to the same Assembly.
精彩评论