.net - How exactly does the "Specific Version" property of an assembly reference work in Visual Studio? -
today had closer @ "specific version" property of assembly references in visual studio 2010. after few experiments unexpected results set out learn as possible how property works. so, appears me, not have answers, here attempt @ self-answering question:
how exactly "specific version" property of assembly reference work in visual studio?
it's compile-time property!
one of important things know "specific version" property takes effect @ compile-time , not @ runtime.
what about?
when project built, project's assembly references need resolved in order find physical assemblies build system should use. if "specific version" check performed (see section "when "specific version" checked?"), affects outcome of assembly resolution process:
- the build system locates physical assembly can potentially use
- the build system compares physical assembly's version assembly version stored in .csproj file assembly reference
- if 2 assembly versions same, resolution process succeeds , physical assembly found used build
- if 2 assembly versions not match, physical assembly discarded , resolution process continues locating next potential assembly
- if no more potential physical assemblies can located, resolution process fails. results in compiler warning (warning msb3245) tells reference not resolved.
- interestingly enough, the build continues! if code has no actual references assembly, build succeeds (with mentioned warning). if code has references, build fails error looks if code using unknown types or namespaces. indication why build really failed warning msb3245.
order in assemblies resolved
the order in assembly resolution process locates potential assemblies appears this:
- the assembly referenced
<hintpath>
element in .csproj file - the project output path
- the gac
note if several versions of assembly exist in gac, resolution process first attempts resolve assembly highest version. important if "specific version" check not made.
when "specific version" checked?
visual studio bases decision whether perform "specific version" check on 2 pieces of information found in .csproj file:
- the presence or absence of
<specificversion>
element, , value (if present) - the presence or absence of version information in assembly reference
this how typical assembly reference version information looks like:
<reference include="foo, version=1.2.3.4, culture=neutral, processorarchitecture=msil"> <specificversion>true</specificversion> <hintpath>..\..\bar\foo.dll</hintpath> </reference>
and how assembly reference looks without version information:
<reference include="foo"> [...]
the following table shows when "specific version" check performed, , when not.
| version information | present not present ----------------------------+------------------------------ <specificversion> | - present, has value true | yes (1) yes (check fails) (2) - present, has value false | no (3) no (4) - not present | yes (5) no (6)
the surprising thing here no check performed if both <specificversion>
, version information absent (case 6). have expected check performed , fail (same case 2) because in understanding absence of <specificversion>
implies default value "true". may quirk of visual studio 2010 did tests.
when examine properties of assembly reference in visual studio ui (select reference , hit f4), value see "specific version" property tells whether or not visual studio going perform "specific version" check. in case 6 ui show "true", although <specificversion>
element not present in .csproj file.
side-effects on "copy local"
if "copy local" property set "true" assembly resolution process fails because of "specific version" check, no assembly copied.
reference material
- what need know referenced assemblies in vs2005 (blogs.msdn.com article)
- what's new in .net 2.0 assemblies , versioning? (codemag.com article reproduces above msdn article, down wording, contains few screenshots , additional information assembly versioning)
Comments
Post a Comment