views:

66

answers:

2

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?

A: 

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.

Femaref
I did check the docs, thank you. I know that these two names "resolve to the same assembly" (as far as ReferenceMatchesDefinition is concerned), but I don't know how that can be. The docs say: "Both reference and definition are resolved by the loader, including policy evaluation, and then definition is tested for equality to reference." I can't see how these two names would be considered equal.
Fabian Schmied
+1  A: 

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!

Hans Passant
Thanks, here is the relevant text:"A ReferenceIdentity matches a DefinitionIdentity, if and only if the value of all the attributes specified in the ReferenceIdentity match the value of the corresponding attributes of the DefinitionIdentity. If an attribute is missing in the ReferenceIdentity, it matches any value for that attribute in DefinitionIdentity. For example, Ref “name” matches Def “name, culture=neutral”, and Def “name, culture=en-us”. But Ref “name, culture=neutral” does not match Def “name, culture=en-us”."
Fabian Schmied
And: "In CLR we have another special comparison --- binding comparison for Ref-Def matching. In the special binding comparison context, the version number is ignored when the ReferenceIdentity does not contain any public key (token)."
Fabian Schmied
Still, I can't see how the versions given above would match.
Fabian Schmied
I'm with you. The CLR uses IAssemblyName::IsEqual() with the ASM_CMPF_IL_ALL flag, in case that helps. The only way Ref-Def matching makes sense to me is the matching that's done when the CLR probes for an assembly when it is not in the GAC. But that ought to be trivial, just the display name match.
Hans Passant