Referenced assembly references
开发者_JS百科If assembly A (which is executable) references assembly B, should it reference all assembly B's references (even if it doesn't use classes which use these references)?
EDIT: There is now a certain amount of confusion about the circumstances under which indirect references are copied into the output folder of the "main" project. It may well depend on the version of Visual Studio, aside from anything else. Visual Studio 2010 appears to use a certain amount of intelligence in working out which indirectly referenced assemblies might actually be used - but the exact logic is a mystery to me at the moment.
If in your situation all the required assemblies end up being copied, then you might as well leave your list of references short - but it's probably worth looking at whether some indirect references are missing, and whether they're important to you or not. In particular, any references which are present in B but only loaded by reflection may not end up being copied...
Older answer
Not necessarily - if it doesn't use any functionality in B which needs the other references even indirectly then it's fine not to add the extra references. However, this means relying on the implementation details of which features in B require which extra references. I think it's typically safer to add all the references unless there are very clear guidelines from the provider of B as to which references are needed in which cases.
EDIT: Just to be clear, you don't have to add references, so long as B
has all the assemblies it needs available at execution time. The simplest way to make sure they all get copied along with A
is to add references to A
. It's more of a deployment simplification than anything else. If B
's references are being loaded from the GAC anyway (not something I personally like) then it won't matter at all; if they're being loaded locally but you're willing to copy the files explicitly into the deployment folder, that's fine too... but adding the assemblies to the list of A
's assembly references is simpler. (I think it's logical, too - they're still assemblies A
needs in order to execute correctly, even though it's an indirect requirement.)
If there is any possibility that any of the indirectly-referenced assemblies is in the GAC on your build machine, but won't be in the GAC at runtime, then you should add explicit references to those indirectly-referenced assemblies and set CopyLocal=True. This is because indirectly-referenced assemblies that are in the GAC won't be copied to the bin folder. More details in my answer here.
精彩评论