Reference resolution changes in Code Analysis and FxCop – Part 1

Another thing that we changed in Visual Studio 2008 Code Analysis and FxCop 1.36, was the way we resolved and located references.

These changes came down to two distinct behavior changes:

Strong names of references must now match.

Assemblies must now completely match the exact strong name (name, version, culture and public key token) to be considered a match for a reference. Previous versions of Code Analysis/FxCop allowed any assembly with the same simple name to be considered a match for a reference. This could cause it to pick the wrong reference when multiple assemblies with the same name were on the search path, which lead typically to bad consequences when it failed to find members that didn’t exist in the assembly it picked.

Most users should not see any functional change in 1.36 as analysis will just work as normal. However, for those that now have FxCop complaining that it can’t find a reference even though it seems to be present – simply make sure you point FxCop to the same references that you compiled against. Visual Studio users should not need to do anything – the correct references will automatically be used.

Direct references are now required.

Direct references of the assembly being analyzed must now be present, while indirect (references of references) remain optional. Analysis of a target assembly will not begin without the same binaries that it was compiled against being present*, whereas previous versions of Code Analysis/FxCop, would error, but would attempt to continue analysis.

This was a controversial decision, but makes sense when you understand the underlying technical reasons for this change; the metadata in an assembly simply does not provide enough information to provide meaningful analysis for some rules.

For example, assemblies do not contain:

  • Information about base classes that live in another assembly. ImplementIDisposableCorrectly, IdentifiersShouldHaveCorrectSuffix and MarkAttributesWithAttributeUsage are examples of rules that need to know the base class of a type to be able to give significant analysis.
  • Information about alternative overloads that may exist for a called particular method if that method lives in another assembly. For example, SpecifyCultureInfo needs to know if there is an alternate overload for a method that takes a CultureInfo – this requires looking in direct reference.
  • Information about security attributes applied to members and types, if those member and types live in another assembly. The Code Access Security (CAS) rules such as DoNotIndirectlyExposeMethodsWithLinkDemands and MethodSecurityShouldBeASupersetOfType use this information for their analysis.

There a few other reasons not listed above, but basically the decision comes down to FxCop's analysis would be incomplete and incorrect if direct references were not required.

* Unfortunately, there are a couple of very small isolated scenarios where this is not true and FxCop could prompt for references you were never actually compiled against. I’ll talk about these scenarios and workarounds in Part 2.

Published Tuesday, June 10, 2008 7:00 AM by David Kean
Filed under:

Comments

Wednesday, June 11, 2008 8:28 AM by Bill Menees

# re: Reference resolution changes in Code Analysis and FxCop – Part 1

Sorry this is a little off topic, but when will FxCop 1.36 be RTMed?  It's been in Beta 2 for 8 months now.  On its announcement post (blogs.msdn.com/.../fxcop-1-36-beta-released.aspx) you said its release "will likely be at the start of next year."  Did you mean 2009?  :-)

It's coming very soon - we ended up holding off releasing it for a particular feature that I'll talk about in the coming weeks.

Wednesday, June 11, 2008 2:01 PM by Bill Menees

# re: Reference resolution changes in Code Analysis and FxCop – Part 1

Thanks!  I'm looking forward to it.

Thursday, June 12, 2008 9:21 PM by Nick Guerrera

# re: Reference resolution changes in Code Analysis and FxCop – Part 1

Excellent! I remember arguing for this change way back. It's the right behaviour.

Congrats on the new position. I'm very intrigued by MEF. I'm currently working on a C# app which has a very rudimentary extensibility model for now. I will look into integrating MEF soon.

Nick (ex-Microsoft, ex-FxCop developer)

Nick, it's good to hear from you. Yep, there was a little of a push back - but I'm really glad we ended up getting this in for 2008 and FxCop 1.36.
Sunday, August 17, 2008 8:22 PM by Dave's Box

# FxCop 1.36 Released!

After what has to be the longest beta period of a product (after Gmail) - we've finally released

Leave a Comment

(required) 
(required) 
(optional)
(required)