Search code examples
c#reflectionassembly-resolutionassembly-name

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?


Solution

  • 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.