Fabian Schmied
Fabian Schmied

Reputation: 4154

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?

Upvotes: 4

Views: 766

Answers (3)

Fabian Schmied
Fabian Schmied

Reputation: 4154

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.

Upvotes: 5

Hans Passant
Hans Passant

Reputation: 942010

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!

Upvotes: 3

Femaref
Femaref

Reputation: 61467

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.

Upvotes: 0

Related Questions